Skip to main content
Participant
February 10, 2011
Question

Conditional Image Resizing

  • February 10, 2011
  • 5 replies
  • 12458 views

Hi

Ive been using batch processing in photoshop since version 7, however Ive recently found the need to do conditional actions - which apparently can be done with photoshop scripting.

What Im trying to do is resize an image if it is over a certain size (document dimensions that is) but leave it at its original size if not.

If I was coding in C or something Id probably do (forgive the poor pseudo code)

if(document.height*document.width>640000){document.resize("50%");}

Can anybody point me in the right direction for creating this type of script in photoshop CS4?

Many thanks

James

This topic has been closed for replies.

5 replies

Known Participant
July 27, 2015

Hi all

I have created 2 scripts for conditional resizing. Hopefully this will come handy for others with just minimal modifications, or as a starting point for their own script.

This one is a simple conditional resize. You just have to change the desired area dimensions, and change upscale to 'true' if you don't mind images to be upscaled.
Conditional resize image script for Photoshop. No save, only resize to desired area. · GitHub

This one is a bit more complex. It does conditional resize for 2 size versions and then saves each version to a folder.
Conditional resize image script for Photoshop. Creates thumbnail and large version and saves in JPEG. · GitHub

If you know nothing about programming and need some little change (or something doesn't work) please tell me.

schroef
Inspiring
April 25, 2017

thanks, super useful. was looking for something which could check if its horizontal or portrait. Finally got it!         

Participant
April 16, 2011

Thanks Jimmery, that was the main part of the mistake and it gave me a better understanding of how to get the script running properly. I ended up with -

app.displayDialogs = DialogModes.NO;
var strtRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var testset2 = '1800 Batch Final';
var taction2 = '1800+ Resize';

if (activeDocument.height < 1800 || activeDocument.width < 1800)
  {
  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  }
else
  {
  app.doAction('1800+ Resize','1800 Batch Final');
  activeDocument.close;
  }
preferences.rulerUnits = strtRulerUnits;

So the action/actionset in the "else" command were around the wrong way also. One more problem I need to overcome, the script would work better if it would only resize images if the shortest dimension was larger than 1800. In its current state it closes images if any dimension is smaller than 1800, yet some images can be less than 1800 in the shortest side yet larger on the longest side. These need to stay open for further processing by the action, so what is the command line for less than on the shortest side? I'll do more research but any help here would be great.

JimmeryAuthor
Participant
April 18, 2011

Maybe try...

var shortestSide;

if (activeDocument.height < activeDocument.width)

{

shortestSide=activeDocument.height;

}else{

shortestSide=activeDocument.width;

}

if (shortestSide < 1800)

  {

  activeDocument.close(SaveOptions.DONOTSAVECHANGES);

  }

else

  {

  app.doAction('1800+ Resize','1800 Batch Final');

  activeDocument.close;

  }

preferences.rulerUnits = strtRulerUnits;

Participant
April 19, 2011

ok - try this instead

var longestSide;

if (activeDocument.height > activeDocument.width) {

longestSide=activeDocument.height;

}else{

longestSide=activeDocument.width;

}

if (longestSide < 1800)

  {

  activeDocument.close(SaveOptions.DONOTSAVECHANGES);

  }

else

  {

  app.doAction('1800+ Resize','1800 Batch Final');

  activeDocument.close;

  }

preferences.rulerUnits = strtRulerUnits;


Nice one that seems to have it cracked, tested on a range of images and it runs perfectly.

Thank you for the persistence and solution, much appreciated. and a great tool to have in my workflow.

Cheers Shaun.

Participant
April 15, 2011

Hi I am trying to create a similar script, which will close images that are opened and are smaller than 1800, but pass on anything over 1800. I have most of the script sorted but it is coming up with an error. Here is the script as I have saved and tested it -

app.displayDialogs = DialogModes.NO;
var strtRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var testset2 = '1800 Batch Final';
var taction2 = '1800+ Resize';

if (activeDocument.height < 1800 || activeDocument.width < 1800)
  {
  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  }
else
  {
  app.doAction(1800 Batch Final,1800+ Resize);
  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  }
preferences.rulerUnits = strtRulerUnits;

I am using Photoshops "Batch" processor to run this script as part of an action, on a folder of randomly sized images, so that if the image opened is bigger than 1800 it gets passed onto another action to resize the image, save and close. My action set is called "1800 Batch Final" and the action within this set that is supposed to be played on files larger than 1800 is called "1800+ Resize". The error message that comes up is -

Error Line 13 - app.doAction(1800 Batch Final,1800+ Resize);

I am new to scripting so any help to get this working would be great, thanks.

JimmeryAuthor
Participant
April 15, 2011

Its been a long time since I did any scripting in Photoshop, however maybe you need to change line 13 to something like:

app.doAction('2500 Batch Final','2500+ Resize');

? Not sure if this helps - sorry!

JJMack
Community Expert
Community Expert
February 10, 2011

If you have CS5 you can actually do what what you want in an action. Adobe added an option to their FitImage plugin script to not resize images the are  that all ready fit  within an image size. So they are not upsized. All Larger images will be be resized to fith with in an image size.  They did also incude a bug in that code if it hasn't been fixed by now some day they may.  Its easy to fix on your own.  Since I modified FitImage to resample using differen methods of bicubic depending on direction I fixed the bug in my bersion.

JJMack
Muppet_Mark-QAl63s
Inspiring
February 10, 2011

I have not looked at that… but the above is based on image 'area' and not longest edge.

Muppet_Mark-QAl63s
Inspiring
February 10, 2011

Is that right that you want to 1/4 the area?

if (app.activeDocument.width.as('px')*app.activeDocument.height.as('px') > 640000) {      app.activeDocument.resizeImage(undefined,undefined, app.activeDocument.resolution/2, ResampleMethod.BICUBICSHARPER); }

JimmeryAuthor
Participant
February 10, 2011

Yes, I want to halve the height and halve the width, which would result in a quarter of the area.

Many thanks for your helpful answer!