Apple's Automator: compression settings for jpg?
Asked Answered
H

5

12

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?

Holiness answered 22/5, 2012 at 12:33 Comment(0)
H
6

Automator’s “Crop Images” and “Scale Images” actions have no quality settings – as is often the case with Automator, simplicity trumps configurability. However, there is another way to access CoreImage’s image manipulation facilities whithout resorting to Cocoa programming: the Scriptable Image Processing System, which makes image processing functions available to

  1. the shell via the sips utility. You can fiddle with the most minute settings using this, but as it is a bit arcane in handling, you might be better served with the second way,
  2. AppleScript via Image Events, a scriptable faceless background application provided by OS X. There are crop and scale commands, and the option of specifying a compression level when saving as a JPEG with

    save <image> as JPEG with compression level (low|medium|high)
    

    Use a “Run AppleScript” action instead of your “Crop” / “Scale” one and wrap the Image Events commands in a tell application "Image Events" block, and you should be set. For instance, to scale the image to half its size and save as a JPEG in best quality, overwriting the original:

    on run {input, parameters}
        set output to {}
        repeat with aPath in input
            tell application "Image Events"
                set aPicture to open aPath
                try
                    scale aPicture by factor 0.5
                    set end of output to save aPicture as JPEG with compression level low
                on error errorMessage
                    log errorMessage
                end try
                close aPicture
            end tell
        end repeat
        return output -- next action processes edited files.
    end run
    

    – for other scales, adjust the factor accordingly (1 = 100 %, .5 = 50 %, .25 = 25 % etc.); for a crop, replace the scale aPicture by factor X by crop aPicture to {width, height}. Mac OS X Automation has good tutorials on the usage of both scale and crop.

Howbeit answered 22/5, 2012 at 14:16 Comment(3)
Thank you for this precise answer but unfortunately all these things doesn't really say anything to me. I never worked with AppleScript. I am also wondering why this is happening anyways cause nothing in my automation asks for a compression.Holiness
@melros: Automator actions are designed for simplicity, not configurability, which means you mostly are at the mercy of the decisions of the action developer as to their implementation.Howbeit
Thank you for your detailed answer! This actually brought me on a new path which I will definitely discover more in future!Holiness
P
13

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!

Partridge answered 24/10, 2012 at 19:29 Comment(1)
Thank you Eric! Any chance you still have the workflow script itself to share?Inspirational
H
6

Automator’s “Crop Images” and “Scale Images” actions have no quality settings – as is often the case with Automator, simplicity trumps configurability. However, there is another way to access CoreImage’s image manipulation facilities whithout resorting to Cocoa programming: the Scriptable Image Processing System, which makes image processing functions available to

  1. the shell via the sips utility. You can fiddle with the most minute settings using this, but as it is a bit arcane in handling, you might be better served with the second way,
  2. AppleScript via Image Events, a scriptable faceless background application provided by OS X. There are crop and scale commands, and the option of specifying a compression level when saving as a JPEG with

    save <image> as JPEG with compression level (low|medium|high)
    

    Use a “Run AppleScript” action instead of your “Crop” / “Scale” one and wrap the Image Events commands in a tell application "Image Events" block, and you should be set. For instance, to scale the image to half its size and save as a JPEG in best quality, overwriting the original:

    on run {input, parameters}
        set output to {}
        repeat with aPath in input
            tell application "Image Events"
                set aPicture to open aPath
                try
                    scale aPicture by factor 0.5
                    set end of output to save aPicture as JPEG with compression level low
                on error errorMessage
                    log errorMessage
                end try
                close aPicture
            end tell
        end repeat
        return output -- next action processes edited files.
    end run
    

    – for other scales, adjust the factor accordingly (1 = 100 %, .5 = 50 %, .25 = 25 % etc.); for a crop, replace the scale aPicture by factor X by crop aPicture to {width, height}. Mac OS X Automation has good tutorials on the usage of both scale and crop.

Howbeit answered 22/5, 2012 at 14:16 Comment(3)
Thank you for this precise answer but unfortunately all these things doesn't really say anything to me. I never worked with AppleScript. I am also wondering why this is happening anyways cause nothing in my automation asks for a compression.Holiness
@melros: Automator actions are designed for simplicity, not configurability, which means you mostly are at the mercy of the decisions of the action developer as to their implementation.Howbeit
Thank you for your detailed answer! This actually brought me on a new path which I will definitely discover more in future!Holiness
E
3

Comment from '20

I changed the script into a quick action, without any prompts (for compression as well as confirmation). It duplicates the file and renames the original version to _original. I also included nyam's solution for the 'space' problem. You can download the workflow file here: http://mobilejournalism.blog/files/Compress%2080%20percent.workflow.zip (file is zipped, because otherwise it will be recognized as a folder instead of workflow file)

Hopefully this is useful for anyone searching for a solution like this (like I did an hour ago).

Engrain answered 4/7, 2020 at 20:6 Comment(1)
workflow file still working in 2023 under macOS Ventura 13.2.1, bedankt!Billetdoux
G
2

Eric's code is just brilliant. Can get most of the jobs done. but if the image's filename contains space, this workflow will not work.(due to space will break the shell script when processing sips.) There is a simple solution for this: add "Rename Finder Item" in this workflow. replace spaces with "_" or anything you like. then, it's good to go.

Gage answered 24/12, 2013 at 0:23 Comment(0)
D
0

Comment from '17

To avoid "space" problem, it's smarter to change IFS than renaming. Back up current IFS and change it to \n only. And restore original IFS after the processing loop.

ORG_IFS=$IFS
IFS=$'\n'
for file in $@
do
    ...
done
IFS=$ORG_IFS
Diaghilev answered 11/2, 2017 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.