Change image resolution (dpi) with AppleScript or Automator

Is there a way to change the resolution of an image (the DPI, not the size) using AppleScript or Automator. To be clear with the terms, I want to change images to 72 dpi, not change their width and height. (I already know how to scale in AppleScript and Automator.) I’m a little surprised that there’s no obvious way to do this.

The sips command can do it:

sips -s dpiHeight 72.0 -s dpiWidth 72.0 test.png

Make sure the values are given as floating point rather than integer.

Since sips is closely tied to AppleScript’s Image Events feature, I would think there’s a way to do it directly in AppleScript, but maybe not.

2 Likes

Thank you, Dr. Drang. I’ll give that a try. Right now, I’m trying to figure out how to pass the path to the file from Automator to the shell script and back without getting an error message.

I’m a little surprised that you can do this from the shell but not from Applescript. (I’ve checked everywhere and while you can find out the resolution, you can’t change it.)

Everything can be done from a shell. I adore AppleScript more than it warrants, but I do see a lot of AppleScripts that are 90% do shell script and wonder why they weren’t just written as shell scripts.

You need only check the Image Events scripting dictionary, which states that the resolution property for an image object is read-only. I am at a loss to explain why that restriction is there. It can be done using AppleScriptObjC, but the shell command is much simpler and more efficient, and probably quicker.

To do what you’re looking to do, you’d create a Quick Action that accepts files as input from within Finder. The workflow would consist of a single Run Shell Script action that would receive input as arguments. This would assign each file path to the shell variables $1, $2, ... etc.

Reading the manpage for sips, I see it accepts multiple file paths as arguments, which saves us having to loop through the files. Collectively, the variables "$1" "$2" "$3" ... when written on the command line as individual quoted arguments can be expressed with the shorthand "$@".

Therefore, your entire shell script would look like this:

sips -s dpiHeight 72.0 -s dpiWidth 72.0 "$@"

I believe this would overwrite the files with the new property key values. However, if they don’t, we’d probably need to specify a value for the --out option that sips provides to allow a folder path to be passed at which new copies of each image file would be saved set with the new resolution.

As a small side note, if the shell script action reports being unable to locate the sips command, you’d need to refer to it using its full path, which can be got by running the command which sips.

Thank you, chri.sk, that seems to have been what I needed to learn to make it work. I most often only need to do this with one file at a time but I included the argument variables. I’m okay with overwriting the file because in a previous step I duplicate it in the Finder.

Thanks again to everyone for their help. I’m glad to have learned something new as much as accomplished the task.

hi to everyone! i’m Italian, sorry for my English.
so i need to change the dpi and the resolution of a lot of images for my “the frame” tv. it needs a specific resolution: 3840x2160 @ 300 dpi.
to do this i made a quick action with automator.
i wrote this commands:

sips --resampleHeightWidth 2160 3840 "$@"
sips -s dpiHeight 300.0 -s dpiWidth 300.0 "$@"

the resolution works but the dpi not. can someone help me? the original images are higher or lesser with dpi but no works in any cases.
thank you!

Does sips do upscaling? If not maybe that’s part of @Iistelladx’s problem.

And welcome, @listelladx!

hi, thanks for the welcome ^.^
i think that sips does upscaling. I red on internet some other ways to write the dpi code with sips but no-one works :frowning:

hi, i’ve found a solution by myself. i don’t know why but if you write more instruction in different lines, automator don’t read all the instructions.
after a thousand of test i found this solution. i write it here so maybe can help some other people.
you can write dimension and resolution that you need.

–resampleHeightWidth 2160 3840 --setProperty dpiHeight 300.000 --setProperty dpiWidth 300.000 “$@”

2 Likes

Glad you found some success … I tried your command in automator (minus the “–resampleHeightWidth 2160 3840”) but I couldn’t get it to change the dpi.

Can you please advise how I can change my DPI to 300dpi without changing the image dimensions and without it resaving the files (they are .jpg and I don’t want to jeopardise the image quality by resaving).