Highlighted

Canvas Resize to Square for a large number of images using script. E.g. image is currently 1020 x 600, I would like to change this to 1020 x 600. PLEASE HELP

New Here ,
May 31, 2014

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Chuck Uebele | Adobe Community Professional

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))

Views

16.9K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Canvas Resize to Square for a large number of images using script. E.g. image is currently 1020 x 600, I would like to change this to 1020 x 600. PLEASE HELP

New Here ,
May 31, 2014

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Chuck Uebele | Adobe Community Professional

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))

Views

16.9K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 31, 2014 0
New Here ,
May 31, 2014

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
Adobe Community Professional ,
May 31, 2014

Copy link to clipboard

Copied

Are the images all the same size of 1020px?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
New Here ,
May 31, 2014

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
Adobe Community Professional ,
May 31, 2014

Copy link to clipboard

Copied

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".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
New Here ,
May 31, 2014

Copy link to clipboard

Copied

Nope didn't want to crop any of the images, just need to get the both height and width for each image to match, so in other words, adding white space to the height or the width to make it square

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
Adobe Community Professional ,
May 31, 2014

Copy link to clipboard

Copied

I think you'll have to run them in same size batches. Then it's just an action with Image > Canvas Size, the units set to pixels, and canvas extension color white. Run the action as a batch (File > Automate > Batch).

If they're all different sizes you could standardize first with File > Automate > Fit Image. This will fit both portrait and landscape to a single "longest side".

You can nest one action inside another if you need.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 0
Adobe Community Professional ,
May 31, 2014

Copy link to clipboard

Copied

Save the following code as a plain text with the extension .jsx or just .js (I can't remember is CS3 was using jsx.  Put the file in the Presets\Scripts folder of Photoshop.  You should then be able to batch run the script by creating an action then running that action in a batch.  I also can't remember is the resize command in CS3 used pixels or just percent, so see if it works on a sample image.

#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))

doc.resizeImage(1020,1020)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 3
New Here ,
Nov 15, 2018

Copy link to clipboard

Copied

This worked PERFECTLY! I can't say how happy I am that I found this script.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2018 0
New Here ,
Jul 09, 2019

Copy link to clipboard

Copied

Mate! That script is awesome! Thanks so much!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 09, 2019 0
New Here ,
Aug 12, 2020

Copy link to clipboard

Copied

Hello all. I've found this script from Chuck very useful, thank you! However as I tweak to fit my needs, I'm encountering some issues. I have followed recommendations here & saved my script and made an action that runs script. From Bridge, I go Tools-->Photoshop-->Batch and run that action. But I get a dialog box come up for Preferences-->units & rulers that I need to act on & not sure what why I get that at all?

 

Alternativelty, I go Tools-->Photoshop-->Image Processor & have that that run the action/script too. This seems to run entire thing smoothly, but at end it gives a dialog saying "sorry I couldn't process the following files". Though the actual resulting files are really there and look correct...

 

Anyone else enouter this or have recommendation what to try? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 12, 2020 0
Most Valuable Participant ,
May 31, 2014

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 31, 2014 3
New Here ,
Jun 01, 2014

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2014 0
Adobe Community Professional ,
Jun 01, 2014

Copy link to clipboard

Copied

If you don't need them all at 1020 and just want them square with the longest side the square size, delete the last line of the script that I posted above.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2014 0
Most Valuable Participant ,
Jun 01, 2014

Copy link to clipboard

Copied

As csuebele wrote you can square all image with his Re-size canvas step.   If you don't want all images to be 1020x1020 if there is a size you would like all to be  just change his re-size image step or change the Fit Image size in the action I described.   You may want to specify the interpolation method in the scripts re-size image step. When none is specified as coded the  method will most likely be your Photoshop Preferences default interpolation method.  Adobe set that by default to Bicubic Automatic.  IMO that is not a good default for when downsizing it seems to use Bicubic Sharper which seem to add sharping artifact to image that have been sharpened.   Fit Image always uses Bicubic which I feel is a better general default.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2014 0
New Here ,
Jun 01, 2014

Copy link to clipboard

Copied

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))

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2014 0
Adobe Community Professional ,
Jun 01, 2014

Copy link to clipboard

Copied

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))

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2014 1
New Here ,
Jun 01, 2015

Copy link to clipboard

Copied

Hi.  Have come across the above and found it very useful.  Is there any way that I can automate the script so that I can run it on multiple images (around 6000) without having to go in to each one individually?

Apologies, probably quite basic, I'm very new to photoshop, using CS5

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2015 0
Adobe Community Professional ,
Jun 01, 2015

Copy link to clipboard

Copied

The easiest way to do this would be to create an action that runs the script. Then use either the batch script or Image processor to run the action/script on open files or on a folder of files.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2015 0
New Here ,
Jun 01, 2015

Copy link to clipboard

Copied

That's great, many thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2015 0
New Here ,
Oct 17, 2017

Copy link to clipboard

Copied

Hi JJMack - how do you "batch the action"?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 17, 2017 0
Most Valuable Participant ,
Oct 17, 2017

Copy link to clipboard

Copied

With Automate batch or one of the Image Processor scripts

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 17, 2017 0
New Here ,
Aug 16, 2019

Copy link to clipboard

Copied

This script helped me a lot! Thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 16, 2019 0
Community Beginner ,
Jan 06, 2020

Copy link to clipboard

Copied

Hello, I seem to get an issue. When I try to run script while recording action I get this:

 

Error 1302: No such element

Line: 9

->  var doc = activeDocument


Any asuggestions? I saved it as .js and put it in scripts (Using 2020)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Adobe Community Professional ,
Jan 06, 2020

Copy link to clipboard

Copied

open any doc

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Community Beginner ,
Jan 06, 2020

Copy link to clipboard

Copied

Not to sure what you mean? Sorry I am quite new with scripts

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Adobe Community Professional ,
Jan 06, 2020

Copy link to clipboard

Copied

You do not have at least one open file in Photoshop.
 
 
 
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Community Beginner ,
Jan 06, 2020

Copy link to clipboard

Copied

Ahh got it thank you so much for getting back to me so quickly. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Zainaziz LATEST
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

can somebody tell me that how I can use the script above in photoshop. I'm beginner and want to know how I can use the above text in Photoshop for batch automation

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0