Sub topic 13
Automating Resizes with a Photoshop Script
This chapter assumes you can open and save Photoshop documents, use the Layers panel, and distinguish pixel dimensions from print resolution. You will use ExtendScript, Photoshop’s JavaScript-based scripting system, to repeat image and canvas resizing consistently.
Why script a resize?
A manual resize works well for one image. A script helps when you must apply the same dimensions to many documents or repeat a multi-step setup. A script can:
- Set the document’s ruler units.
- Calculate a proportional height from a target width.
- Resample an image with a chosen interpolation method.
- Extend or shrink the canvas.
- Use a fixed anchor position.
A script does not make a low-resolution image contain more real detail. It only applies the same resizing rules reliably.
`resizeImage` and `resizeCanvas`
Photoshop provides separate methods for two different jobs:
resizeImage()changes the image dimensions. When the new dimensions require a different number of pixels, Photoshop resamples the image.resizeCanvas()changes the working area around the artwork. It does not scale the existing pixels.
The anchor position controls where the existing artwork sits when the canvas changes.
A complete ExtendScript example
Save the following code as a file ending in .jsx. Open a document in Photoshop, then choose File > Scripts > Browse and select the script.
// Photoshop ExtendScript (.jsx)
if (app.documents.length === 0) {
alert("Open a document before running this script.");
} else {
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
// Resample the image to 1200 px wide while preserving its proportions.
var targetWidth = 1200;
var newHeight = Math.round(
doc.height.value * targetWidth / doc.width.value
);
doc.resizeImage(
UnitValue(targetWidth, "px"),
UnitValue(newHeight, "px"),
72,
ResampleMethod.BICUBICSHARPER
);
// Extend the canvas by 100 px on every side.
doc.resizeCanvas(
UnitValue(doc.width.value + 200, "px"),
UnitValue(doc.height.value + 200, "px"),
AnchorPosition.MIDDLECENTER
);
alert("Resize complete.");
}The first condition checks whether a document is open. app.documents.length reports the number of open documents. If that number is zero, the script shows a message and stops.
app.activeDocument refers to the document currently selected in Photoshop. The script stores that document in the doc variable so later commands are easier to read.
The proportional-height calculation uses the original aspect ratio:
new height = old height × target width ÷ old width
Math.round() converts the result to a whole number of pixels. The script then calls resizeImage() with a target width of 1200 pixels. The height is calculated, so the image does not become stretched.
The 72 argument sets the resolution value to 72 pixels per inch. Because the width and height are specified in pixels, the important operation here is the pixel resampling. ResampleMethod.BICUBICSHARPER tells Photoshop which interpolation method to use. Interpolation is the method Photoshop uses to estimate pixel values when it adds or removes pixels.
The final command increases both canvas dimensions by 200 pixels. That creates 100 pixels of extra space on each side when the artwork remains centered by using AnchorPosition.MIDDLECENTER.
Common scripting errors
- No document is open: Accessing
app.activeDocumentwithout checking can cause an error. - Wrong units: A document may use inches or centimeters. Setting
app.preferences.rulerUnits = Units.PIXELSmakes the dimension calculations predictable. - Distorted proportions: Passing an arbitrary width and height to
resizeImage()can stretch the image. Calculate one dimension from the other. - Unexpected canvas placement: A different anchor position moves the artwork toward that anchor as the canvas changes.
- Repeated quality loss: Running a resampling script several times can reduce quality. Keep an original file and resize from it when possible.
Exercise
You need an image to be 1600 pixels wide without changing its proportions, then want 50 pixels of extra canvas on every side. Which methods should you use, and what canvas dimensions should you pass if the resized image is 1600 by 900 pixels?
Show answer
Use resizeImage() first with 1600 by 900 pixels. Then use resizeCanvas() with 1700 by 1000 pixels and AnchorPosition.MIDDLECENTER.