Skip to main content
May 31, 2014
Answered

Canvas Resize to Square for a large number of images using script.

  • May 31, 2014
  • 3 replies
  • 35066 views

Hi All,

 

I have a large number of images that I need to resize the canvas sizes to a square, the images are currently in different sizes. For example, if an image is 1020 x 600 I would like to change the canvas to 1020 x 1020 so that the image becomes a square. I am using CS3 and all the images are jpeg's. I have done research on scripts but the ones I have tried have not worked. Please help.

 

Thanks.

 

 

Apologies, in the title I meant, if the image is 1020 x 600 i would like to change the canvas to 1020 x 1020.

This topic has been closed for replies.
Correct answer Chuck Uebele

Just tried that but line 12 comes up with an error:

 

Error 25: Expected: ).

Line: 12

->

doc.resizeCanvas(Math.max(doc.width,doc.height),Math.max(doc.width,doc.height))

You may have deleted something you shouldn't have.  Here's the script again without the last line:

 

#target photoshop
app.preferences.rulerUnits = Units.PIXELS;

var backgroundC = new SolidColor();
backgroundC.rgb.red = 255;
backgroundC.rgb.green = 255;
backgroundC.rgb.blue = 255;
backgroundColor = backgroundC;

var doc = activeDocument
doc.resizeCanvas(Math.max(doc.width,doc.height),Math.max(doc.width,doc.height))

 

3 replies

Participant
August 28, 2021

Here is the solution... This script changes the canvas to square by using the longest side as the maximum dimension
save the file as .jsx  Then record an action by running the script in an open document. Play the action created with batch process.

var doc = activeDocument
app.preferences.rulerUnits=Units.PIXELS
// Evaluating for landscape document
if (doc.width > doc.height){
	doc.resizeCanvas(doc.width, 0 + doc.width, AnchorPosition.MIDDLECENTER)
} else {
	doc.resizeCanvas(0 + doc.height, doc.height, AnchorPosition.MIDDLECENTER) //for portrait document
}
Participant
November 5, 2021

Is there also a way to customize the scrip so it's add a border of lets say 10% so that the image is scaled down a bit in the sqaure?

JJMack
Community Expert
Community Expert
November 5, 2021

After the script Crops the image using that  Canvas Size  step you  could add a second  canvas size step to the script to add some canvas to the width and height to add your 10% boarder. If the document has a background layer its boarder will have colored pixels in the boarder.  If the Document does not have a background layer the script can add a new bottom layer and fill it with any color your want. Its just a matter of adding some more code.

doc.resizeCanvas(doc.width*110/100, doc.height*110/100, AnchorPosition.MIDDLECENTER)
JJMack
JJMack
Community Expert
Community Expert
June 1, 2014

Since you do not want to crop your images to a square 1:1 aspect ratio changing the canvas to be square will not make your images square they will retain their Aspect Ratio and  image size will be changer to fit within your 1020 px square. There will be a border or borders on a side or two borders on opposite sides.   You do not need a script because Photoshop ships with a Plug-in script to be used in Actions.   What is good about Plugins is the support Actions.  When you record the action the plug-in during action recording records the setting you use in its dialog into  the actions step.  When the Action is played the Plug-in use the recorded setting an bypasses displaying its dialog. So the Action can be Batch.  The Action you would record would have two  Steps.   Step 1  menu File>Automate>Fit Image... in the Fit Image dialog enter 1020 in the width and height  fields.  Step 2 Canvas size enter 1020 pixels in width and height  not relative leave the anchor point centered it you want even borders on two sides set color to white in the canvas size dialog. You can batch the action.

The above script will also work. Its squares the document then re-sizes to 1020x1020  the action re-sizes the image to fit with in an area 1020 x 1020 then add any missing canvas. The script like the action only process one image so it would also need to be batched. Record the script into and action and batch the action. As the author wrote. The script re size canvas did not specify an anchor point so the default center anchor point is uses  like the action canvas will be added to two sides.

JJMack
June 1, 2014

All the images I have are of different sizes, so I don't need to change them all to 1020 x 1020 specifically, as some images are higher than that and some are lower res. I'm thinking there is some sort of script that will use an IF statement to increase a dimension to whichever dimension is higher for a image.

Kukurykus
Legend
April 9, 2022

@Chuck Uebele – Thanks for confirming!

 

OK, I like to capture and return the original foreground colour, so I have incorporated your more concise code into my previous function.

 

EDIT: Updated with Chuck's 2nd shorter code!

 

setBackgroundPicker();

function setBackgroundPicker() {
    // interactive background color picker
    var origFGC = foregroundColor;
    app.showColorPicker(true);
    backgroundColor = foregroundColor;
    foregroundColor = origFGC;
}

 

 


 

backgroundColor=[foregroundColor,foregroundColor=backgroundColor][0]

 

Chuck Uebele
Community Expert
Community Expert
May 31, 2014

Are the images all the same size of 1020px?

May 31, 2014

The images I have vary in size, I need to make 8000 images square, some are 175 x 65 some are 1644 x 1280.

D Fosse
Community Expert
Community Expert
May 31, 2014

Just to get it out of the way - do you want to crop to make them square, or add black borders? Obviously you can't take a 16:9 ratio and "make it square".