2

I have been looking around for an Automator method, whereby an image file is to be resized (reduced height and width) to a specific file size (in bytes), or a near lower-bound proximity of that file size.

It would be a great use for MacOS Mojave's Quick Actions, as many website uploads do not accept full-size iPhone images, for instance.

My intuition is that this is a quite difficult problem for Automator to solve even with the help of bash scripting, etc. Usually, even applications like Adobe Photoshop are not great at predicting the final file size after a file is compressed to JPG or PNG, etc. Perhaps it could incrementally lower the image dimensions until it's lower than the max file size. Easiest would be if was a scriptable utility for this.

I therefore thought to ask if:

  • a) Is this doable, and
  • b) How would it be done?
  • To clarify, you're referring to a file's compression before it effects the image? You could write a script to detect if the image is portrait or landscape, resize to a safe size like 800px and then compress. – ʀ2ᴅ2 Nov 26 '18 at 12:22
  • @ʀ2ᴅ2 Thanks for asking. Resizing an image to a fixed size via Automator is covered in various tutorials on the web and is not difficult. I was more thinking of having it variably be resized to approximate a specific file size (e.g. optimizing the size to the desired file size). – Winterflags Nov 26 '18 at 12:31
2

I'm a little confused with the approach on how to properly compress the images you want to work with. As mentioned, there is sips, a working example based on pulling a directory of images and stepping through the list with a resize if the width isn't 800px:

property theWidth : 800

tell application "Finder"
    try
        ## Choose directory
        set imageDir to choose folder with prompt "Please select directory."

        ## Get list of items in diretory
        set the imageList to list folder imageDir without invisibles

        ## Step through images
        repeat with theImage in imageList

            ## Turn path to posix path
            set imagePos to quoted form of POSIX path of ((imageDir as text) & contents of theImage)

            ## Get image width
            set imgWidth to (do shell script "sips -g pixelWidth " & " " & imagePos & " " & "| tail -n1 | cut -d' ' -f4") as integer

            ## if not equal to set variable width resize
            if imgWidth ≠ theWidth then do shell script "sips --resampleWidth " & theWidth & " " & imagePos

        end repeat
    on error error_message
        display dialog error_message
    end try

That is just an example of what you can do with the width. As mentioned in the comment you could check to see which is longer the width or height and resize from there. If you do man sips in the Terminal you can pull all types of info. The above code uses tail and cut to trim the return. Resizing is done with --resampleWidth. You could resample based on height with --resampleHeight.

Regarding the compression aspect, if these are PNGs you can use Optipng. If you want something you can run with PNGs and JPGs you could use Trimage. AppleScript/Automater could also be scripted into using a compression utility. You would just need to call from bin a check if a compression exists:

try
    set checkBin to "(ls usr/local/bin/optipng >> /dev/null 2>&1 && echo yes) || echo no"
    return do shell script checkBin
on error error_message
    return error_message
end try

The above is checking for OptiPNG. If you wanted to check wether something was installed with HomeBrew you could use a similar command on /usr/local/Cellar.

All the above mentioned could be completely done in a Shell Script. It just depends on what you want and how you want to execute the code.

  • 1
    Consider using quoted form of when dealing with file paths. This will avoid problems with spaces, filenames containing quotes, and escape characters. – Graham Miln Nov 26 '18 at 14:37
  • it was in there originally but looks like I removed it on breaking down the code. will edit. – ʀ2ᴅ2 Nov 26 '18 at 14:43
1

Yes, with a shell script

You could write a shell script to do what you want and wrap that within an Automator workflow.

In Automator only, this is not possible. Automator lacks the branching logic required to determine when to stop.

I am not aware of any shortcuts, other than to resize and compress differing size images until they meet your goal.

This can be done with built-in tools included with macOS, such as sips for resizing the image and bash for the scripting the logic.

See Reduce image file size to certain size for a closely related question.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .