Skip to main content
tammarab1083
Participant
October 16, 2018
Answered

Create script to use canvas size to square the image by the largest dimension.

  • October 16, 2018
  • 2 replies
  • 4312 views

I would like to square images.  The images have various rectangular dimensions.  I would like each image to be canvased by the largest dimension then automate the process for each photo and it's largest dimensions.

automated example

7000 X 3005  to  7000 x 70000

5454 x 2500  to  5454 x 5454

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

hi im using the script but I want to change the last part to instead of using backgound color to use content aware fill 


Here is a script to make the image square, using the longest side of the image, then using content aware fill.

#target photoshop

var savedRuler = app.preferences.rulerUnits; //save the current ruler units

app.preferences.rulerUnits = Units.PIXELS; 

var doc = activeDocument

var newSize = Math.max(doc.width.value,doc.height.value);
var topS,leftS,bottomS,rightS

if(doc.width>doc.height){
    topS = -(newSize-doc.height.value)/2;
    leftS = 0;
    bottomS = (newSize-doc.height.value)/2 + doc.height.value;
    rightS = doc.width.value;
    cropContentAware ();
    }
else if(doc.height>doc.width){
    topS = 0;
    leftS = -(newSize-doc.width.value)/2;
    bottomS = doc.height.value;
    rightS = (newSize-doc.width.value)/2 + doc.width.value;    
    cropContentAware ();
    }
else{}

app.preferences.rulerUnits = savedRuler; //restore the original ruler units

function cropContentAware(){
    var idCrop = charIDToTypeID( "Crop" );
        var desc363 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var desc364 = new ActionDescriptor();
            var idTop = charIDToTypeID( "Top " );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc364.putUnitDouble( idTop, idPxl, topS );
            var idLeft = charIDToTypeID( "Left" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc364.putUnitDouble( idLeft, idPxl, leftS );
            var idBtom = charIDToTypeID( "Btom" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc364.putUnitDouble( idBtom, idPxl, bottomS );
            var idRght = charIDToTypeID( "Rght" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc364.putUnitDouble( idRght, idPxl, rightS );
        var idRctn = charIDToTypeID( "Rctn" );
        desc363.putObject( idT, idRctn, desc364 );
        var idAngl = charIDToTypeID( "Angl" );
        var idAng = charIDToTypeID( "#Ang" );
        desc363.putUnitDouble( idAngl, idAng, 0.000000 );
        var idDlt = charIDToTypeID( "Dlt " );
        desc363.putBoolean( idDlt, false );
        var idautoFill = stringIDToTypeID( "autoFill" );
        desc363.putBoolean( idautoFill, true );
        var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );
        var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );
        var idpureAspectRatio = stringIDToTypeID( "pureAspectRatio" );
        desc363.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idpureAspectRatio );
        var idCnsP = charIDToTypeID( "CnsP" );
        desc363.putBoolean( idCnsP, false );
    executeAction( idCrop, desc363, DialogModes.NO );    
    }

2 replies

Stephen Marsh
Community Expert
Community Expert
October 16, 2018

A forum search kicked this one up (thank you Chuck Uebele). The script creates a white background on flattened images and transparency on layered images:

https://forums.adobe.com/message/6424562#6424562

I have added a couple of lines of code to save/restore the original ruler units.

//https://forums.adobe.com/message/6424562#6424562

//https://forums.adobe.com/message/10683707#10683707

#target photoshop

var savedRuler = app.preferences.rulerUnits; //save the current ruler units

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) //Uncomment to fit to a 1020px square rather than longest edge

app.preferences.rulerUnits = savedRuler; //restore the original ruler units

Prepression: Downloading and Installing Adobe Scripts

The script can be recorded into an Action and used with the Batch command or with Image Processor or Image Processor Pro for bulk processing. Test on a small number of images before attempting to process 40K!

Participant
October 24, 2021

Just found your post and it was exactly what I needed! Thank you.

JJMack
Community Expert
Community Expert
October 16, 2018

If all the files are flat image file without transparency a relatively easy Photoshop script you can write could do the deed.  Processing 40,000 image files will take a long time. If the files may be layered image file that may transparency adding canvas may add transparent canvas boarders and tthe image will not look like a 1:1 aspect ratio  square image, Using such an image in a composite using Photoshop tools may trim off transparent borders. 

I believe an action can not do the deed for an Action can not use logic to examine side sizes and use  logic to add the right amount of canvas to the  right side of the image..

If you nee help writing the script there is a Photoshop scripting forum Photoshop Scripting

JJMack
tammarab1083
Participant
October 16, 2018

Thanks  I was hoping for some automation of the process to square each image by its largest dimension .

JJMack
Community Expert
Community Expert
October 16, 2018

After you write it it will be.

JJMack