• 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

31.3K

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
New Here ,
Sep 14, 2020 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

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
Engaged ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Hey @Chuck Uebele !

 

First of all, thanks for sharing this script. It indeed helps a certain workflow where I have to resize the canvas of dozens of pictures! 🙂

 

Now my question (request):

Is it possible to interrupt the script to let us pick the background color, maybe via Photoshop's color picker and after that, the script continues and resizes the canvas?

I know how to change the color inside the script itself, but if I want a particular color, instead of creating a new script and having to reboot PS for it to be loaded, it would be super useful if we could pick the color in real time. This wouldn't be useful for a droplet with dozens or hundreds of images, of course, but for a few images, it would be great!

 

Let me know if that can be done.

I can look at JS and kinda (big emphasis on "kinda")  understand what's going on, but I'm no expert at all, especially when it comes to the specifics of PS...

 

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 ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

I would use scriptListener to get the code for changing the background. At the end of the code there is a line for showing the dialog box. Change this from NONE to ALL bit make sure you set it back to NONE.

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
Engaged ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Thank you for the tip. Unfortunately, I'm really not an expert (not even close, to be honest) so that sounds like a task that's too advanced for my current knowledge...

 

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 ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied


@TiagoRocha wrote:

Thank you for the tip. Unfortunately, I'm really not an expert (not even close, to be honest) so that sounds like a task that's too advanced for my current knowledge...


 

Don't worry Danny, others may step in!

 

@Chuck Uebele – that was my first thought, but it is not that simple... I have tried with two versions of Photoshop, with the raw SL code and converted code through Clean SL and no GUI is presented.

 

This code works for the foreground:

 

getColorpickerColorFore();

function getColorpickerColorFore() {
    if (app.showColorPicker()) {
        return app.foregroundColor;
    } else {
        return false;
    }
}

 

 

 

However, strangely, this does not, it returns the Foreground colour:

 

getColorpickerColorBack();

function getColorpickerColorBack() {
    if (app.showColorPicker()) {
        return app.backgroundColor;
    } else {
        return false;
    }
}

 

 

Coding error? A bug? Programming hole? 

 

This leaves me with the following, which works, but seriously should one really have to jump through these hoops?

 

setBackgroundPicker()

function setBackgroundPicker() {
    var foreCol = app.foregroundColor;
    getColorpickerColorFore();
    exchange();
    app.foregroundColor = foreCol;

    function getColorpickerColorFore() {
        if (app.showColorPicker()) {
            return app.foregroundColor;
        } else {
            return false;
        }
    }

    function exchange() {
        var c2t = function (s) {
            return app.charIDToTypeID(s);
        };
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var reference = new ActionReference();
        reference.putProperty(s2t("color"), s2t("colors"));
        descriptor.putReference(c2t("null"), reference);
        executeAction(s2t("exchange"), descriptor, 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
Community Expert ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Yea, @Stephen_A_Marsh Looks like you have to bring up the colorpicker, it sets the foreground color, then you switch the foreground with the background.

 

app.showColorPicker(true);
var idExch = charIDToTypeID( "Exch" );
    var desc12 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idClr = charIDToTypeID( "Clr " );
        var idClrs = charIDToTypeID( "Clrs" );
        ref2.putProperty( idClr, idClrs );
    desc12.putReference( idnull, ref2 );
executeAction( idExch, desc12, 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
Community Expert ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

@Chuck Uebele – Thanks for confirming!

 

OK, I like to capture and return the original foreground colour, so I have incorporated your more concise code into my previous function.

 

EDIT: Updated with Chuck's 2nd shorter code!

 

setBackgroundPicker();

function setBackgroundPicker() {
    // interactive background color picker
    var origFGC = foregroundColor;
    app.showColorPicker(true);
    backgroundColor = foregroundColor;
    foregroundColor = origFGC;
}

 

 

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
LEGEND ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

 

backgroundColor=[foregroundColor,foregroundColor=backgroundColor][0]

 

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 ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied


@TiagoRocha wrote:

it would be super useful if we could pick the color in real time. This wouldn't be useful for a droplet with dozens or hundreds of images, of course, but for a few images, it would be great!


 

Here you go, some adjustments to Chuck's script, obviously presuming a flattened image:

 

EDIT: Updated with Chuck's second, shorter code!

 

#target photoshop;

if (app.documents.length) {

    var bgExists = true;
    try {
        activeDocument.backgroundLayer;
    } catch (e) {
        bgExists = false;
    }

    if (bgExists === true) {

        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
        setBackgroundPicker();
        var doc = activeDocument;
        doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height));
        //doc.resizeImage(1020, 1020);
        app.preferences.rulerUnits = savedRuler;

    } else {
        app.beep;
        alert("This script requires a Background layer!");
    }

} else {
    app.beep;
    alert("A document must be open to use this script!");
}

function setBackgroundPicker() {
    // interactive background color picker
    var origFGC = app.foregroundColor;
    app.showColorPicker(true);
    app.backgroundColor = app.foregroundColor;
    app.foregroundColor = origFGC;
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

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 ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

@Stephen_A_Marsh if you really want to reduce the code, you can use the below that stores the original foreground color, then puts it back at the end of the script. It bypasses exchanging the BG & FG code, my just asigning those values using the DOM.

var origFGC = foregroundColor;
app.showColorPicker(true);
backgroundColor = foregroundColor;
foregroundColor = origFGC;

 

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 ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

Cool, obvious in hindsight, I have always used eXchange for manual use and actions so that was my go to in scripting.

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
Engaged ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

@chuckoand @Stephen_A_Marsh you guys are amazing for sharing this! Thank you so much!

Using the latest code provided by Stephen, I was able to pick the color and make it work, so for what I need this script for at the moment, this works for me.

But while we are at it, I have 2 questions for Chuck:

1 - You say that your latest code could be added at the end of the code Stephen shared. I'm not exactly sure where that code would go so can you share the full code with yours include, please?

2 - I'm guessing that what you mean is that if I have, for example, green as background, if I pick orange as my color while using the script, at the end I would have the green back as my background, instead of orange, right? Right now it seems that it keeps the picked color as background. Can you confirm?

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 ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied


@TiagoRocha wrote:

But while we are at it, I have 2 questions for Chuck:

1 - You say that your latest code could be added at the end of the code Stephen shared. I'm not exactly sure where that code would go so can you share the full code with yours include, please?

 

I have updated my two previous posts with the shorter version of the function as discussed with Chuck.

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
Engaged ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Thanks again!

It's working with the new code, but it's doing the same as yours. If I have red as my background color, for example, if I then pick green as my color for the borders when resizing the canvas, it applies the green as my canvas color (so far so good), but after the script is done running, it keeps the green as the background color, not the red. Is it what it's supposed to do or did I misunderstand what Chuck said?

I thought it would store the original color in memory and once the script was done running, it would put it back (in this case, the red). It's not a big deal. As I mentioned, it's working great the way it is, but if that was the purpose, there's something that's not working, in case someone else would like to use the 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
Community Expert ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied


@TiagoRocha wrote:

Thanks again!

It's working with the new code, but it's doing the same as yours. If I have red as my background color, for example, if I then pick green as my color for the borders when resizing the canvas, it applies the green as my canvas color (so far so good), but after the script is done running, it keeps the green as the background color, not the red. Is it what it's supposed to do or did I misunderstand what Chuck said?

I thought it would store the original color in memory and once the script was done running, it would put it back (in this case, the red). It's not a big deal. As I mentioned, it's working great the way it is, but if that was the purpose, there's something that's not working, in case someone else would like to use the script 🙂


 

 

No, you misunderstood.

 

The problem is that scripting does not appear to allow access to the background colour picker GUI, only the foreground. Therefore, as a hack, we have to call the foreground picker GUI and then exchange the foreground for the background. By storing a reference to the original foreground and then returning this reference, we have the end result of the background colour changing via the GUI and the foreground colour not changing, just as would happen outside of scripting. It is a convoluted hack to work around a design flaw or bug in scripting.

 

Edit: It is now understood that it is possible to call the background picker via scripting.

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
Engaged ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Oh ok, I see what you mean. By default, when we use the picker it sets the foreground color, you're right!

 

So, using variables would it be possible to do what I assumed it was doing?

For example:

1 - check the hexa value of the current background color and set a variable with a name such as "originalBackgroundColor"

2 - run the rest of the script

3 - at the end of the script it reverts the background color back to whatever hexa value is set by "originalBackgroundColor"?

 

 

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 ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied


@TiagoRocha wrote:

Oh ok, I see what you mean. By default, when we use the picker it sets the foreground color, you're right!

 

So, using variables would it be possible to do what I assumed it was doing?

For example:

1 - check the hexa value of the current background color and set a variable with a name such as "originalBackgroundColor"

2 - run the rest of the script

3 - at the end of the script it reverts the background color back to whatever hexa value is set by "originalBackgroundColor"?


 

Yes, a very minor alteration:

 

    if (app.documents.length) {

    var bgExists = true;
    try {
        activeDocument.backgroundLayer;
    } catch (e) {
        bgExists = false;
    }

    if (bgExists === true) {

        var origBGC = app.backgroundColor;
        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;
        app.showColorPicker(false);
        var doc = activeDocument;
        doc.resizeCanvas(Math.max(doc.width, doc.height), Math.max(doc.width, doc.height));
        //doc.resizeImage(1020, 1020);
        app.preferences.rulerUnits = savedRuler;
        app.backgroundColor = origBGC;

    } else {
        app.beep;
        alert("This script requires a Background layer!");
    }

} else {
    app.beep;
    alert("A document must be open to use this script!");
}

 

Note: Code updated to include the suggestion from Kukurykus

 

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
Engaged ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

YES!!! That worked like a charm! 🙂 Awesome!

Thank you again for your great contribution and help! I really hope other people who need this, find this thread.

 

I don't know if there's a public place where people can share their scripts, but if there is, you should definitely add this, because I'm sure others would find it super useful!

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
LEGEND ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

If you're aware you can set true parameter to showColorPicker for foregroundColor why don't you change setBackGroundPicker function to showColorPicker(false) for backgroundColor?

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 ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

Putting false does work! I was going to try that, when I was playing around with the code, but just did get to it. 

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 ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied


@Kukurykus wrote:

If you're aware you can set true parameter to showColorPicker for foregroundColor why don't you change setBackGroundPicker function to showColorPicker(false) for backgroundColor?


 

I had already mentally moved on and didn't test further when Chuck presented the code.

 

misleading.png

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
LEGEND ,
Apr 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

LATEST

It's undocummented indeed, so I had no idea it's possible until I found out in your code a true parameter. I was like what? Why anyone would put it in? Now we know the true parameter is default, so parentheses can be left empty, but with the other goal the false parameter is surely needed. It's a bit odd because normally 'no parameter' used is like false was put in, not true 😕

 

Aside of the above, at Apr 09, 2022 I presented a code I thought up myself many years ago before I start scripting in Photoshop. It let you exchange background with foreground colour without using ActionManager code, also without storing and later retrievieng original values.

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

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

With Automate batch or one of the Image Processor scripts

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