Sub topic 8
Making Actions Work on Images of Any Size
This chapter assumes you can record and play an action, that you know the Layers panel and the Image Size dialog, and that you know the difference between pixels and percentages. It covers why an action that works on one file breaks on another, and how to record it so it works on all of them.
Why actions break on different images
An action stores the values you used while recording. If you record "Image Size, width 1600 px" on a 4000 px photo, the action replays that exact number on every file. That is fine for resizing. It fails when a step depends on the image's shape, such as a crop at fixed pixel coordinates, a text layer at fixed position, or a canvas extension sized for one photo.
Most failures come from four causes:
- Absolute pixel values recorded for positions and sizes.
- Different orientations (landscape and portrait) in one batch.
- Different color modes, bit depths or layer structures.
- Steps that target a layer or channel by name when that name is missing.
The goal is to make each step depend on something that is the same for every file, such as a percentage, an edge of the canvas, or a selection derived from the image itself.
Use relative values instead of absolute ones
Where a dialog lets you choose a unit, choose the one that scales with the image.
| Task | Fragile recording | Portable recording |
|---|---|---|
| Resize | Width 1600 px, height 1067 px | Percent, or width only with Constrain Proportions on |
| Canvas extension | Add 200 px of canvas | Relative option on, or percent units |
| Crop | Drag a marquee at fixed coordinates | Use Select All, then Transform Selection, then Crop |
| Position a layer | Drag by eye | Align to selection or canvas |
Resizing
In Image Size, turn on Constrain Proportions and set only the dimension you care about. If you want all images no wider than 1600 px, a single recorded width of 1600 px gives you that width, with height following the aspect ratio. Be aware of two consequences:
- Portrait images will also be set to 1600 px wide, so they end up very tall. If you want a limit on the long side, use the Fit Image command (File > Automate > Fit Image) instead. It takes a width and a height as a bounding box and fits any image inside it.
- Images smaller than the target are enlarged. Fit Image has a Don't Enlarge option in some versions. If yours does not, use a conditional action (a later chapter) or filter by size before the batch.
Canvas Size
In Canvas Size, turn on Relative and enter an amount. The amount is still absolute (for example 50 px on each side). For padding that scales, choose Percent as the unit. Percent is the more portable choice for borders.
Selections and crops
Recording a marquee drag stores pixel coordinates. A better way is to build the selection from the document itself:
- Select > All.
- Select > Transform Selection, then enter a percentage width and height (for example 90%) from the center reference point.
- Image > Crop.
Because the percentages apply to the current canvas, the same recording works on any size.
Handle orientation
Landscape and portrait files in one batch are the usual source of surprises. There are three strategies.
- Choose an operation that does not care. Fit Image and percentage resizing treat both orientations correctly.
- Split the batch. Sort files into two folders and run a different action on each.
- Branch inside the action. A conditional action can test whether the document is landscape or portrait and then play a different action. This is covered in the chapter on conditional actions.
The following diagram shows how to decide which strategy fits.
Standardize the starting state
An action assumes a certain starting state. If the file differs, a step may fail or produce a different result. Add steps at the start of the action to normalize the document.
- Color mode. Add Image > Mode > RGB Color if some files may be CMYK or Grayscale. If the document is already RGB, Photoshop skips the change without an error in most cases, but check this with your own files.
- Bit depth. Add Image > Mode > 8 Bits/Channel before filters or formats that need 8-bit data. JPEG saving requires it.
- Color profile. Use Edit > Convert to Profile with sRGB for web output, so colors look the same on every file.
- Layers. If the action sets a layer's properties, begin with a step that selects a known layer, such as the top layer or the Background.
Selecting layers needs care. If you recorded a click on a layer named "Layer 1", the action looks for that name. Layers are better selected with the relative shortcuts Alt+] (Option+] on Mac) for the next layer up, Alt+[ for the next layer down, or Select > All Layers. These do not depend on the name.
Create what the action needs
A reliable pattern is for the action to build every object it relies on, instead of expecting it to be present. For example, a watermark action can:
- Place a linked or embedded watermark file with File > Place Embedded.
- Scale the placed layer by a percentage of the canvas.
- Align the layer to the lower right using the align commands with the selection set to the whole canvas.
Because the layer is created on every run, it exists with a known name, and alignment is relative to the canvas.
Note that File > Place Embedded records the path of the file you placed. If you move the watermark, the action fails. Keep it in a stable location, and consider storing it next to the action set.
Account for resolution
Pixel dimensions and resolution are separate. A 3000 px image can be 300 ppi or 72 ppi. If your action sets a print size in inches, the result depends on the stored ppi. To make this predictable, uncheck Resample when you change only resolution, and check it when you change the pixel dimensions. Record both steps explicitly so the action does not depend on whatever the file contained.
Test with extreme files
Before running a batch on hundreds of files, test the action on a small set that includes the awkward cases:
- The largest and the smallest image.
- One portrait and one landscape image.
- A square image.
- A file with multiple layers and a flattened file.
- A file in a different color mode.
Play the action on each and check the result. Fix each failure by replacing the absolute step with a relative one, or by adding a normalizing step at the beginning.
You can step through the action to find a failing step. In the Actions panel, choose Playback Options from the panel menu and select Step by Step. Photoshop runs one command at a time, which shows which step produces a wrong result.
Add a stop for judgment calls
Some decisions cannot be turned into rules. In those cases, use Insert Stop, as covered in the earlier chapter on inserting stops. The message tells the user what to do, and you can enable Allow Continue. In a Batch run, stops pause the process for every file, so use them only for manual work, not for unattended processing.
Worked example: a web export action
This action prepares any photo for the web. Record it on a flattened RGB file, then check it on the test folder.
- Image > Mode > RGB Color.
- Image > Mode > 8 Bits/Channel.
- Edit > Convert to Profile, destination sRGB.
- File > Automate > Fit Image, 1600 px by 1600 px.
- Filter > Sharpen > Unsharp Mask, with low values such as Amount 60%, Radius 0.8 px, Threshold 0.
Fit Image bounds both sides at 1600 px, so portrait and landscape files both come out with a long side of at most 1600 px. The mode, depth and profile steps remove differences in the starting state.
Here is the same idea as a script, for when you outgrow actions. It limits the long side to 1600 px without enlarging:
#target photoshop
var doc = app.activeDocument;
var maxSide = 1600;
var w = doc.width.as("px");
var h = doc.height.as("px");
var longSide = Math.max(w, h);
if (longSide > maxSide) {
var scale = maxSide / longSide;
doc.resizeImage(
UnitValue(Math.round(w * scale), "px"),
UnitValue(Math.round(h * scale), "px"),
doc.resolution,
ResampleMethod.BICUBICSHARPER
);
}The script reads the real dimensions from the document, so it adapts to every file. This is the same principle as the relative recording described above.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Crop lands in the wrong place | Marquee recorded at fixed coordinates | Build the selection from Select All and a percentage |
| Portrait images are far too tall | Width-only resize | Use Fit Image |
| "Layer not found" style error | Layer selected by name | Use relative layer selection |
| Small images are blurry | Upscaled to the target size | Skip them with a condition |
| Result differs in CMYK files | Mode not normalized | Convert mode and profile first |
Exercise
You recorded an action that crops with a marquee drag and resizes to a width of 1200 px. It works on landscape photos but ruins portrait photos. Name two changes that make it portable.
Show answer
Replace the marquee drag with Select All followed by Transform Selection using percentage values, then Crop, so the crop scales with each canvas. Replace the width-only resize with Fit Image using a 1200 by 1200 px bounding box (or branch with a conditional action), so the long side is limited regardless of orientation.
Exercise
Why should an action that depends on a layer start with a step that selects the layer by position or creates it, instead of clicking it by name?
Show answer
A step recorded on a named layer fails when a file has no layer with that name, and the batch stops. Selecting by relative position, or creating the layer inside the action, guarantees the layer exists with a known name in every file.