Auto Crop

JavaScript for Adobe Photoshop
Latest update 9/6/2020, version 1.2.1

The script crops images using Photoshop’s Select Subject feature and adds a minimum margin to arrive at defined dimensions with the subject centered. The process may be performed on the active image or a folder of images.

This solution grew out of my answer to a post on the Adobe Forums (seach for “Script help: Crop based on select subject”). The code I had posted was a minimum to answer to the user’s question. As time has allowed, I’ve gotten around to packing the code into a complete script with interface offering further controls, which I now offer here.

  • Define image dimensions and minimum margin
  • Uses Select Subject to determine cropping
  • Process active image or folder of images
  • Resolution as-is or resample
  • Options to convert to profile and/or flatten
  • Append suffix identifying cropped size
  • Processing folder outputs to JPG, PNG, PSD, or TIF
  • Save and restore all settings
Auto Crop screen
Download
Auto Crop

You decide. Reward the author an
amount the solution is worth to you.

Instructions for use

The interface has five sections: Process, Size, Resolution, Output, and Settings. Enable desired options and click the OK button to begin. If processing the active image, any errors or concerns are reported immediately. If processing a folder, a progress bar is displayed. To cancel processing, press the ESC key. Otherwise, when complete, the number of images processed is reported. If any errors or concerns are encountered, a log file is written to the input folder and the user is notified.

If the dimensions and minimum margin produce an image that does not reach one or more edges of the final cropped dimensions, an alert is added to the log file. Use this information to find images that need content-aware fill added either prior to or after cropping.

The script depends on the accuracy of Photoshop’s Select Subject feature, which works well in most cases, but isn’t perfect 100% of the time. At a minimum, the user should open Bridge and scan through thumbnails of cropped images to ensure the results are satisfactory. For any images that crop improperly, open in Photoshop and crop manually.

Section 1: Process

Active image — processes the image that is currently open and the top-most window if multiple images are open. When this option is selected, the section Output is disabled. The active image is cropped but not flattened, and the image is resampled if the option is selected, but options in the section Output are ignored. It is up to the user to perform additional transformations if needed and Save As the cropped version of the image in the desired format. A revised file name should be used to preserve the original un-cropped version of the image.

Folder — Processes a folder of images. If Photoshop encounters non-image files that it cannot open, alerts are reported in the log file. For each image file found in the selected folder, the script crops the image and performs the specified options in the sections Resolution and Output.

Upon selecting a folder to process, the output folder in the section Output will update to the same folder plus a subfolder named “Cropped.” To override, click the Folder button in the Output section and select/create the desired folder to which cropped images are saved.

Section 2: Size

Width and Height — set as desired. To the right of Width is a drop down list of meaurement units. Set as desired. Measurement units for the remaining inputs automatically update to the same. When changing measurement units, the current values for dimensions and margin are automatically converted to the new meausrement units.

Minimum margin — a measurement from the subject that is the smallest margin once cropped. Some margins may be greater than this value depending on the cropped width and height, but no margin is ever less than this value.

Section 3: Resolution

As-is (no interpolation; Pixels/Inch derived from new size) — the Pixels/Inch value changes as a result of cropping and resizing the image to the desired dimensions, but no image data is resampled. The pixels of the image after cropping are precisely the same as the pixels before cropping/resizing, only less the pixels cropped away.

Resample to original Pixels/Inch — after the image is cropped, the image is resampled to the original Pixels/Inch value of the image before cropping/resizing. In the event this up-scales the resolution, an alert is added to the log file.

Resample — enter a value in pixels per inch. After the image is cropped, the resolution is adjusted to the specified value. In the event this up-scales the resolution, an alert is added to the log file.

Regarding “aspect ratio” versus size: the option As-is exists to satisfy users asking for cropping based on aspect ratio. This option changes the image size, but lets the resolution adjust to whatever value that size makes it, without removing any pixels beyond what cropping requires. The real size of any image is its pixel dimensions, not inches, millimeters, etc. Those are real-world units of measure for the benefit of us humans. Without resampling pixel data, sizes expressed in inches (for example) simply set how many pixels represent one inch. The point is, using the As-is option makes specifying size identical to specifying an aspect ratio. The only pixel data lost is the portion cropped away to arrive at the desired ratio of width relative to height.

Section 4: Output

Format — choose from four common file formats: JPG, PNG, PSD, or TIF.

JPEG quality (JPG format only) — valid range is from 0 to 12. 0 is extreme compression resulting in low quality. 12 is light compression that is virtually indistinguishable from the original, the highest possible quality, which of course, results in the largest file size. 10 to 12 is recommended for print or other high-quality reproduction. For web images, 5 to 8 is an acceptable range.

Convert to profile — the cropped image is converted to the specified profile. Note that PNG always converts to sRGB web standard. All other formats embed either the original color profile or the profile to which the image is converted.

Original file name + — a suffix of characters appended to each cropped image file name. The characters entered must be legal to use in file names. Any illegal characters are automatically removed. As Width and Height values are changed, the suffix is updated to match. Having no suffix is allowed, and if desired, the suggested value may be cleared once sizes are set and before processing.

Flatten — while processing an image, the background is converted to a layer so that cropped pixels are preserved while resizing the canvas to arrive at the desired cropping. If not flattened, these pixels remain but are hidden from view. Enable this option to remove the excess, and/or because a single background layer is the desired result. Note that some formats and situations always flatten the image, for example output to JPEG, which does not support layers. The PNG format is always converted to sRGB web standard and this can also trigger flattening. As well, images converted from RGB to a CMYK profile may flatten to preserve the effect of adjustment layers, which cannot convert from one color space to another.

Folder — the location to which cropped images are saved. The script sets the folder when selecting the Folder option in Process section. To override, click the Folder button in the Output section and select/create the desired folder to which cropped images are saved. When processing the Active image, this value is ignored as the script does not save changes to the active image, rather leaves that to the user.

When cropped images are output, any existing cropped images of the same name are replaced without alert.

Section 5: Settings

The current options may be saved and restored later. Select from the Load drop-down list to choose saved settings, which then updates the current options. Click the Delete button and the saved settings selected in the Load drop-down list are permanently removed. Click the Save button, provide a name for the settings, and the current options are preserved. If the name already exists, the user may choose to replace the saved settings.

The script provides default saved settings named [Default]. These settings cannot be deleted but may be updated to any desired values by saving settings and providing the name [Default], which then overwrites the default settings.

Each time processing begins, the current options are preserved, and the next time the script is launched, options are restored to the last values used.

Available color profiles

For the option Convert to profile, the list of profiles from which to choose is compiled by searching known locations in the system for .icc/.icm files and extracting the profile name. This occurs each time the script is launched. The list should include most of the same profiles Photoshop displays in dialogs such as Color Settings, but it doesn’t match exactly. If a needed profile does not appear in the list, add the profile to a location both Photoshop and the script look for profiles (below) and relaunch the script.

macOS

:Library:ColorSync:Profiles

Windows

\Windows\System32\spool\drivers\color

Download
Auto Crop

License details included in download

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

IMPORTANT: by downloading the script you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.

CHANGE LOG

Version 1.2.1 (200906)

  1. PNG and untagged JPG ensure profile is removed.
  2. Convert indexed color to 8-bit sRGB.

Version 1.2 (200831)

  1. Remove excess colorProfileNames.indexOf.
  2. Revise closeWasNotOpen function to preserve unsaved docs.
  3. Fix error check if image fits crop, loop missing property length.
  4. Miscellaneous.

Version 1.1 (200828)

  1. When testing if image fills crop area, test all layers.
  2. Work around ExtendScript bug: set bg color changes shape layer fill.
  3. Add UI verbiage to resolution option As-is.
  4. Add resolution option resample to original Pixels/Inch.

Version 1.0 (200826)

  1. Initial release.