• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

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

Guest
May 31, 2014 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.

 

 

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

TOPICS
Actions and scripting

Views

28.6K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 01, 2014 Jun 01, 2014

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

 

Votes

Translate

Translate
Adobe
Community Expert ,
May 31, 2014 May 31, 2014

Copy link to clipboard

Copied

Are the images all the same size of 1020px?

Votes

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
community guidelines
Guest
May 31, 2014 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.

Votes

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
community guidelines
Community Expert ,
May 31, 2014 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".

Votes

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
community guidelines
Guest
May 31, 2014 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

Votes

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
community guidelines
Community Expert ,
May 31, 2014 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.

Votes

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
community guidelines
Community Expert ,
May 31, 2014 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)

Votes

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
community guidelines
New Here ,
Nov 15, 2018 Nov 15, 2018

Copy link to clipboard

Copied

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

Votes

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
community guidelines
New Here ,
Jul 09, 2019 Jul 09, 2019

Copy link to clipboard

Copied

Mate! That script is awesome! Thanks so much!

Votes

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
community guidelines
New Here ,
Aug 12, 2020 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? 

Votes

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
community guidelines
Community Expert ,
May 31, 2014 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

Votes

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
community guidelines
Guest
Jun 01, 2014 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.

Votes

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
community guidelines
Community Expert ,
Jun 01, 2014 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.

Votes

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
community guidelines
Guest
Jun 01, 2014 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))

Votes

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
community guidelines
Community Expert ,
Jun 01, 2014 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))

 

Votes

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
community guidelines
Guest
Jun 01, 2015 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

Votes

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
community guidelines
Community Expert ,
Jun 01, 2015 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.

Votes

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
community guidelines
Guest
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

That's great, many thanks

Votes

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
community guidelines
New Here ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

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?

 

Votes

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
community guidelines
New Here ,
Aug 16, 2019 Aug 16, 2019

Copy link to clipboard

Copied

This script helped me a lot! Thanks!

Votes

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
community guidelines
Community Beginner ,
Jan 06, 2020 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)

Votes

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
community guidelines
People's Champ ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

open any doc

Votes

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
community guidelines
Community Beginner ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
People's Champ ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines