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

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

New Here ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

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

Views

3.1K

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 2 Correct answers

Community Expert , Oct 16, 2018 Oct 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.rulerUnit

...

Votes

Translate

Translate
Community Expert , Oct 24, 2022 Oct 24, 2022

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

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

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

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 ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

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

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 ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

After you write it it will be.

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
New Here ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

I don't see how it will do it.  can u give a mini step by step please

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 ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

The logics is strait forward. The script logic would go like this.  You would record using the script in an Action the patch that action.

save current units

set units to pixels

If active document width not equal active document height {

   if active document width is greater than active document height canvas size active document width, active document width; 

   else canvas size active document height, active document height;

   active document save

}

restore saved units

Image that were are not square would be made square and saved.

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
Contributor ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

take a look at IrfanView. i think it can do what you want.

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 ,
Oct 17, 2018 Oct 17, 2018

Copy link to clipboard

Copied

Sorry for the delay  i left work.   I  see thank you ...   I'm new to adobe  So  I have to download Adobe script and then run this script.   I see.

Thank You

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 ,
Oct 17, 2018 Oct 17, 2018

Copy link to clipboard

Copied

No Adobe ScriptingSupport was an optional plug-in download for Photoshop Version 7.  It was incorporated into Photoshop in Photoshop CS and is in all version of Photoshop since  Photoshop CS.  If you look at your Menu Help>System info you should see that it is loaded.  Adobe uses Photoshop scripting for many things. Like Image Processor, load files into a stack, export layer to files.  You just need to write a script or use the one posted here.  The one posted here does not save any document. It would need to be recorded in an action and batched with menu File>Automate>Batch and have batch save document into a destination.  Or use menu File>Scripts>Image Processor it you want all Image file saved as a PSD or Jpeg or Tiff or all three types.   If you want a different File type saved you can download the Plug-In Image Processor Pro... and use menu File>Automate>Image Processor Pro to batch your action

40,0000 is a lot of files I can not think  of any reason someone would need 40,000 square Image files with different sizes some of which would have borders on top and bottom other with borders on left and right  where some border will be transparent  and others will be colored and some image file may have no boarders

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
New Here ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

Hi! is there a way to put in the script to fill with content aware after making it square? or and automated action to choose the white and then delete to content aware? because all files are different sizes so and action doesnt work with all. 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 Expert ,
Oct 16, 2018 Oct 16, 2018

Copy link to clipboard

Copied

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!

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 ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

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

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

Hi! is there a way to put in the script to fill with content aware after making it square? or and automated action to choose the white and then delete to content aware? because all files are different sizes so and action doesnt work with all. 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 Expert ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

There should be a way to do this, but depending on the size to be filled, the results may not be very good.

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

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

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

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

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

Hi are amazing it works !!!! 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 Expert ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

LATEST

@susanah26730930lk45 – I have marked Chuck's script as a correct reply for you.

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