Skip to main content
Known Participant
April 30, 2024
Question

Create resize action to only apply to images bigger than set parameters

  • April 30, 2024
  • 1 reply
  • 533 views

I need to resize a large folder of images but only resize the ones that are bigger than a specified set of dimensions. If I was scripting this it would be simple, something like this.

if image_height > x:
  image_width = x / (image_height / image_width)
  image_height = x

 I'm not sure of the process of scripting in photoshop though so, rather than fumble my way around the api for a while, is there a simpler way to do this? I was imagining creating an action that I could apply to a folder.

This topic has been closed for replies.

1 reply

Legend
April 30, 2024

There are four ways to script/program for Photoshop. We won't consider the C++ API or CEP scripting which is deprecated, so the most common ways now are UXP and Extendscript.

In Extendscript, this is easy:

var doc = app.activeDocument;
preferences.rulerUnits = Units.PIXELS;
var maxSize = 600;
if(doc.width > maxSize){
   doc.resizeImage(width: UnitValue, height: UnitValue, resolution: number, resampleMethod: ResampleMethod, amount:int);
   }

 

In doc.resizeImage() you would need to specify desired settings. You could also use doAction() to call a recorded action instead.

DativeAuthor
Known Participant
April 30, 2024

Thanks very much for this! Extendscript is a new one on me, is there a place I can use this script within photoshop or do I just have to save a file in the scripts folder? I'd got this far with a JS script, though there was a lot I was unsure about.

 

 

const resizer = () => {
	let files = openDialog();
	for file in files {
		let height = file.height.value;
                let maxHeight = 2362;
		let width = file.height.width;
                let resolution = 300
		let newWidth = maxHeight / (height / width);
		file.resizeImage(width, height, resolution, ResampleMethod.AUTOMATIC)
		if file.height.value > maxHeight {
			file.resizeImage(newWidth, maxHeight, resolution, ResampleMethod.AUTOMATIC)
		}
	}
} 

 

– Is there a default unit or do I need to specify pixels?
– Is there a way to set "Interpolate the pixel information" to true or false in image size? For example, if I want to change the resolution to 300 from 350 with the checkbox off, thereby increasing the dimensions of the image, and then turn it on to scale down the image. I assume this is possible but couldn't find the option in the 2019 JS ref docs.

– If I have ResampleMethod.AUTOMATIC selected, do I need a fifth parameter?
– I've noticed most people scripting using var instead of const and let, I haven't coded in JS for a while, is there a reason for this?

Sorry for the barrage of questions, feel free not to answer them! It seems your suggestion of using doAction() will be perfectly suitable so I'll find a way to work around it using that.

Legend
April 30, 2024

Extendscript is a branch of Javascript. Your test script above is NOT correct syntax and won't run.

You can go here for info

https://developer.adobe.com/photoshop/

Also, look through this forum, there are lots of good script examples.

I have a Dropbox with open source scripts and many others do as well, or Github etc.

https://www.dropbox.com/sh/mg817g9a9ymbasi/AADTmXUVxmFfM58bcyYE7yiwa?dl=0