Skip to main content
Participating Frequently
May 15, 2016
Answered

How to obtain the largest value of width and/or height of two open images?

  • May 15, 2016
  • 2 replies
  • 1653 views

Re: Photoshop CS6, ver 13.0.1 (x32) on Win 7 SP1 (64-bit) 16gB RAM

My Photoshop workflow deals with repeatedly editing sets of two images of an irregularly shaped object centered on a white background. Upon completion of the edits, each canvas needs to be cropped, then both resized to square canvas of the same dimensions.

The question I have is the last step: How do I determine the largest single dimension to use as px width, px height when squaring the two open images?

The following script seems to work, but I'm not sure if I am correctly selecting the active documents. Is app.activeDocument = app.documents[0] always the document currently being edited? And app.activeDocument = app.documents[1] the document not active !?

I'm a VBA/VB.Net programmer with no experience in Javascript. I will greatly appreciate expert opinions on the following, my first attempt at a Photoshop script.

#target photoshop
<menu>automate</menu>
//save the current setting
var savedRuler= app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
//set background to white
var backgroundC = new SolidColor();
    backgroundC.rgb.red = 255;
    backgroundC.rgb.green = 255;
    backgroundC.rgb.blue = 255;
    backgroundColor = backgroundC;
//get max dimension of first image
app.activeDocument = app.documents[0] // set to first image
var h = app.activeDocument.height;
var w = app.activeDocument.width;
var maxDim = w;
if(h>w) maxDim = h;
//measure second image then get the max dimension of both images
app.activeDocument = app.documents[1] // set to second image
var h2 = app.activeDocument.height;
var w2 = app.activeDocument.width;
if(w2>maxDim) maxDim = w2;
if(h2>maxDim) maxDim = h2;
//resize second image
app.activeDocument.resizeCanvas (maxDim, maxDim, AnchorPosition.MIDDLECENTER);
//resize first image
app.activeDocument = app.documents[0] // set to first image
app.activeDocument.resizeCanvas (maxDim, maxDim, AnchorPosition.MIDDLECENTER);
//clean up
app.preferences.rulerUnits = savedRuler;

This topic has been closed for replies.
Correct answer JJMack

JJMack,

While 5px is only .1mm at 1200 ppi, apparently the only purpose of that value is to insure no clipping of the image. The photo background is white, the book is printed on white paper. I'm sure the publishers know what they need.

I like your suggestion for automating the entire process and that is my goal. I have received about 5000 pairs of TIFF photos that have white canvas with fairly wide borders from the centered object. Typical dimensions: 2281x1921 pixels at 2500 ppi. I need to trim them, reset to 1200 ppi (not constraining proportions) then make the canvas square. Should be doable -- and that was purpose underlying my question for this thread: I needed to know how to obtain the largest value of width and/or height of two open images so I could set their canvases square. I think I now understand how to use scripting to switch back and forth between two documents open in Photoshop.

Scripting the process to handle thousands of photo pairs in a folder will be a new challenge and probably needs a new thread.


Try This

#target photoshop

//save the current setting

var savedRuler= app.preferences.rulerUnits;

var orig_display_dialogs = app.displayDialogs;

app.preferences.rulerUnits = Units.PIXELS;

app.displayDialogs = DialogModes.NO; // Set Dialogs off

var defaultFolder = "~";

var inputFolder = Folder.selectDialog("Choose a folder of images to process.", defaultFolder);

var outputFolder = Folder.selectDialog("Choose a folder to export resized images to.", defaultFolder);

var fileList = inputFolder.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|jpe|jpeg|tif|tiff|psd|eps|png|bmp)$/i);

if (fileList.length%2===0) { // even number of files

  // Loop through files

  for (var i = 0; i < fileList.length; i++) {

     app.open(fileList); // Open First

     app.activeDocument.trim(TrimType.TOPLEFT);

     app.activeDocument.resizeImage(null,null,1200,ResampleMethod.NONE);

     var h = app.activeDocument.height;

     var w = app.activeDocument.width;

  

     i++;

     app.open(fileList); // open second

     app.activeDocument.trim(TrimType.TOPLEFT);

     app.activeDocument.resizeImage(null,null,1200,ResampleMethod.NONE);

     var h1 = app.activeDocument.height;

     var w1 = app.activeDocument.width;

     var maxDim = Math.max (Math.max (w, w1), Math.max (h, h1)) +10;

     app.activeDocument.resizeCanvas (maxDim, maxDim, AnchorPosition.MIDDLECENTER); //resize

     // save second and close

     if (app.activeDocument.name.lastIndexOf(".")!=0 ) { var Name = app.activeDocument.name.substring(0, app.activeDocument.name.lastIndexOf(".")); }

     else {var Name =  app.activeDocument.name;}

     var saveFile = outputFolder + "/" + Name ;

     SaveAsJPEG(saveFile, 10);

     activeDocument.close(SaveOptions.DONOTSAVECHANGES);     // Close the just save

     app.activeDocument.resizeCanvas (maxDim, maxDim, AnchorPosition.MIDDLECENTER); //resize

     // save and close First

     if (app.activeDocument.name.lastIndexOf(".")!=0 ) { var Name = app.activeDocument.name.substring(0, app.activeDocument.name.lastIndexOf(".")); }

     else {var Name =  app.activeDocument.name;}

     var saveFile = outputFolder + "/" + Name ;

     SaveAsJPEG(saveFile, 10);

     activeDocument.close(SaveOptions.DONOTSAVECHANGES);     // Close the just save

     }

  }

else { alert("Odd Number of Files");}

//clean up

app.displayDialogs = orig_display_dialogs; // Reset display dialogs

app.preferences.rulerUnits = savedRuler;

////////////////////////////////////////////////////////////////////////////////////////////////////////

function SaveAsJPEG(saveFile, jpegQuality){

  var doc = activeDocument;

  if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;

  jpgSaveOptions = new JPEGSaveOptions();

  jpgSaveOptions.embedColorProfile = true;

  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

  jpgSaveOptions.matte = MatteType.NONE;

  jpgSaveOptions.quality = jpegQuality;

  activeDocument.saveAs(File(saveFile+".jpg"), jpgSaveOptions, true,Extension.LOWERCASE);

}

2 replies

JJMack
Community Expert
Community Expert
May 15, 2016

In my opinion my need to re-think what you wrote.

The script you posted does not crop at all and will work if the document have a background layer or your saving a file format that does not support transparency.  All that script does is add additional canvas more white space  around your objects.

I think what you may actually want to do is to size all your image  to be some particular square size.  That would not be hard to script or do with an action.  You must come up the the size you want. One you know that the rest is relatively easy and can be done by batching an action.

You start the action with a step to trim the white space around your irregular shaped objects. The next step fit the image to a square area slightly smaller then the image size you want to wind up with. The next and last step  add canvas to make the image the size you want.

You wind up with images all the same square size which contain your object resize to fit the center of that square size.

JJMack
Participating Frequently
May 15, 2016

JJMack,

Thanks for your comment. However, the trimming/cropping has already been done manually on the two photos in this set before this script is run.

This script is the last step, and is solely to measure width and height of both images and resize the canvases of both to a square of the measured max length. I do not want a pre-determined fixed canvas size: these are 1200 ppi macro photos at 1:1 ratio for print publication at life size. To eliminate size distortion, no resizing of the image is permissible. Having the two canvases of exactly equal square size is needed for insertion (as a pair) into the pre-press text. In my Photoshop workflow, the next set of two photos will likely have entirely different dimensions.

I think the real issue is whether or not I am correctly selecting the active documents. Is that being done properly in my script?

JJMack
Community Expert
Community Expert
May 16, 2016

From reading your appends it seem to me what you seem to want are image that are something like 1200px x 1200px they would be 4" x 4" with a 300DPI resolution.  I download some product image from the web that have white background.  Image that were different in sizes products with different shapes. Products the were even things like white shoes and shirts.  I recorded the action I describe and adding an extra step to set a 300dpi resolution.  I then batched that action using the standard Photoshop Image Processor script.  Here is how it worked.

JJMack
pixxxelschubser
Community Expert
Community Expert
May 15, 2016

Hi Edward C. D. Hopkins,

does this works for you?

//alert (Math.max (Math.max (app.documents[0].width, app.documents[1].width), Math.max (app.documents[0].height, app.documents[1].height)));

alert (Math.max (Math.max (w, w1), Math.max (h, h2)));

Have fun

Participating Frequently
May 15, 2016

pixel,

Forgive if I misunderstand, but doesn't an Alert pop a dialog box? No interaction is needed nor desirable. I simply want the two photos to quietly have their canvases resized to be equal, at the largest dimension.

Was you intention to pop a dialog box?

pixxxelschubser
Community Expert
Community Expert
May 16, 2016

At first:

Do not use an if clause without brackets!

if(h>w) maxDim = h;

should be:

if(h>w) {maxDim = h};

Edward C. D. Hopkins schrieb:

pixxxel,

Forgive if I misunderstand, but doesn't an Alert pop a dialog box? No interaction is needed nor desirable. I simply want the two photos to quietly have their canvases resized to be equal, at the largest dimension.

Was you intention to pop a dialog box?

This was only an example to give you the possibility to check this way with some documents quickly and standalone.

For using my example in your script „replace“ the alert in my snippet with var [your variable] instead of the three if clauses

var maxDim = Math.max (Math.max (w, w1), Math.max (h, h2));

Have fun