Photoshop Script for re-size odd dimension to square canvas size

In this tutorial, I'll explain about how to;
 - Script for Re-size canvas size of a odd dimension image to square(with the longest dimension)
 - How to record a Photoshop action
 - How to load a Script File
 - How to batch process a folder with selection Photoshop action

Step 1

Script for Re-size canvas size of a odd dimension image to square(with the longest dimension)

I've lots of images that are all different sizes. So I needed to create an action that makes these images square in px dimentions, with the new canvas size of the longest side. 
As a example, if we have a image with 720x576 , then the new canvas size will be, 720x720.  

1. So here we have a action script file, which you need to install.

You can download a photoshop action file from here. 

Or you can copy below code to a new text editor and save it as  .jsx file.

This is the code. 

var savedRuler= app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var w = app.activeDocument.width;
var h = app.activeDocument.height;
if(w>h) app.activeDocument.resizeCanvas (w, w, AnchorPosition.MIDDLECENTER);
if(w<h) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);
//if w==h already square
app.preferences.rulerUnits = savedRuler;


2. Save this script file on a separate folder on your computer.


Step 2

How to record a Photoshop action

Now lets see, how to record photoshop action with above script. 

1. Open Photoshop and create a new document and save it. I created a new document in 800x600 size. 


2. Now open Photoshop action panel, Windows - Actions


3. Now you need to create a "Set". Every action must belong to a "Set" which is a folder containing one or more actions. You can do this by clicking fly-out menu, and selecting "New Set".

4. Give a meaningful name on the pop-up window.

5. Now this set willl appear at the bottom of the action palette.  With your action set selected, click on the new action button at the bottom of the action palette. 

6. In the pop-up window, give the new action name. If you would like you can set a Function Key as well. (Shortcut). and click "Record". 

7.  Ok, Now we have started Recording.  You can see "Red color" recording button is active. It means, now photoshop will record every things we do here untill we click stop button. 

8.  Ok, Now these are the steps I needed for my action steps. 
   -  Unlock background the layer. (double clicking on the background layer)
   -  Load the action script file
   - Save

so, lets do this. 

Ok first double click on the background layer, and it will unlock. You can see that action will be recorded.
in to this..
You can see its recording that step under "my-custom-actions" set. 

Now we need to load the script file which we saved earliar. 

Step 3

 - How to load a Script File

1. Go to File - Scripts - Browse   and locate the script file and load it. 


and, Once you Load it, You'll see the photoshop has resized the image. 

In my case, my new canvas size has become 800x800. 

2. Now,most importantly We need to Save the file. 
Go to File- Save  and save it. 
(Remember, this save location will be the location for any file you are applying this action. )


You'll be able to see "Save" step also saving a step on the action set. 


Actually, I added a close step to the action set. 
I clicked on the last save step and Press "Record" button again. and then Close the image. 

Now Click on the Stop button which is beside on the Record button.

Ok Cool, Now we have a action, that we can apply to any image. 
You can test this by opening any image and putting action to it. 

I've opened this image from sample  picture folder from windows 7. 

Now selection the "Odd-image-resize" action from the action pallet and click on the "Play" button. 
You'll see image get resized and saved on the above same location. 

Ok, so we are done. 
If you like, you can add this action to any single image. Or you can do a batch process. 


Step 4

How to batch process a folder with selection Photoshop action

1. Put all the pictures which need to process to a single folder. 

2. Now go to  File--> Automate --> Batch

3. On the batch window, Select your custome set. and then the Action as "odd-image-resize". 

then, Source as "Folder" and click Choose button and select your folder. 

Set the Destination as "Folder" , and create a new folder, I created a new folder as "export" inside same folder and selected it. 

Once you press OK, You'll see all the pictures are opening, and get applied actions and saving. 

So, One thing went wrong..with the saving path. 
as I checked , they were not in the export folder.. :D

but in our earlier folder which I save my document. 
So, actually you can go to action pallete and double click on the save step, and change the save location. 

Post a Comment

14 Comments

  1. thank you! thank you! thank YOU!!!!!

    ReplyDelete
  2. Hi Sanjeewa, great tutorial, I have a question, is it posible to make the script fill up the extra space with a solid color?
    Thanks!

    ReplyDelete
  3. Hey Sanjeewa... your script really helped me a lot :)

    Thanks a ton dude!!!

    ReplyDelete
  4. You are a saviour!!!!!!!!!!! Thank you very much

    ReplyDelete
  5. Thank you so much. Great script!

    ReplyDelete
  6. Thank you so much. I've been searching all over for this and finally you saved me days of manual work.

    ReplyDelete
  7. i cannot thank you enough for this! saved me about 3 weeks!

    ReplyDelete
  8. i cannot thank you enough for this. you have saved me at least a week. 3000 images all being squared up one by one as we speak using my new batch! Thank you!

    ReplyDelete
  9. THANK YOU SO MUCH!!!!

    ReplyDelete
  10. If you change the code on lines 5 and 6 to be reversed then you will get a clean square crop without any empty space top and bottom etc.

    if(wh) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

    ReplyDelete
  11. Reverse lines 5 and 6 and the crop will be a perfect square without any extra space....

    if(wh) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

    ReplyDelete
  12. Perfect instructions. I am slightly less clueless now.

    ReplyDelete
  13. So...I have pictures in a folder to square, and your example creates a new file in Photoshop. (which I assume you leave open as you do all this?) How do I square a batch of existing pictures using the automation feature? These directions don't work for that

    ReplyDelete
  14. I've been trying this for over four hours, I follow every single step, step-by-step and I cannot get this to work. Can you show a simplified step-by-step minus the pictures and telling EXACTLY what to click on, etc?

    Example: 1) Click this, then click that, then click this...

    Your directions do not work.

    ReplyDelete