Apple's Automator: compression settings for jpg?

Melros picture Melros · May 22, 2012 · Viewed 13.8k times · Source

When I run Apple's Automator to simply cut a bunch of images in their size Automator will also reduce the quality of the files (jpg) and they get blurry.

How can I prevent this? Are there settings that I can take control of?

Edit:

Or are there any other tools that do the same job but without affecting the image quality?

Answer

Eric picture Eric · Oct 24, 2012

If you want to have finer control over the amount of JPEG compression, as kopischke said you'll have to use the sips utility, which can be used in a shell script. Here's how you would do that in Automator:

First get the files and the compression setting:

Passing files and compression settings to a shell script in Automator

The Ask for Text action should not accept any input (right-click on it, select "Ignore Input").

Make sure that the first Get Value of Variable action is not accepting any input (right-click on them, select "Ignore Input"), and that the second Get Value of Variable takes the input from the first. This creates an array that is then passed on to the shell script. The first item in the array is the compression level that was given to the Automator Script. The second is the list of files that the script will do the sips command on.

In the options on the top of the Run Shell Script action, select "/bin/bash" as the Shell and select "as arguments" for Pass Input. Then paste this code:

itemNumber=0
compressionLevel=0

for file in "$@"
do
    if [ "$itemNumber" = "0" ]; then
        compressionLevel=$file
    else
        echo "Processing $file"
        filename="$file"
        sips -s format jpeg -s formatOptions $compressionLevel "$file" --out "${filename%.*}.jpg"
    fi
    ((itemNumber=itemNumber+1))
done
((itemNumber=itemNumber-1))
osascript -e "tell app \"Automator\" to display dialog \"${itemNumber} Files Converted\" buttons {\"OK\"}"

If you click on Results at the bottom, it'll tell you what file it's currently working on. Have fun compressing!