Using a Shell Script to Batch Process an Image

Shell scripts can be created in TextEdit and run in the Terminal app. In this example, we'll create a script that takes a single image and produces multiple versions of that image by looping through a series of sizes. This script can be used again and again to reproduce the image series when changes are made to the original.

Comments: 8 Responses to “Using a Shell Script to Batch Process an Image”

    Stephen
    6 years ago

    Absolutely fascinating - so good to see a technical / programming tip every so often! More, please, Gary!

    Steven
    6 years ago

    Very nice!
    So you don't have to make the file executable with chmod anymore (or did I miss that step)?

    6 years ago

    Steven: You only have to make it executable if you want to run it from the Finder. In this example, we are running it from the Terminal.

    Silvio
    6 years ago

    Thanks for these wonderful tutorials.
    A question.
    I did exactly as explained, but the terminal denies me permission:
    MacSilvio: Ridimensiona immagine silviomarcis $ ./ProcessaImmaginee.txt
    -bash: ./ProcessaImmagine.txt: Permission denied
    Naturally, I am logged in as a Director.
    I tried to put before the SUDO command but the result is the same.
    How do I solve?
    Thank you
    Translated from Italian with Google. Sorry

    6 years ago

    Silvio: Do you mean you are logged in as an "administrator?" If so, it should work. But perhaps you have something else going on that is preventing it. If you are feeling adventurous, look into using the chmod command to change the permissions on that file so it will run.

    Silvio
    6 years ago

    Gary: You were right, I added the executable property to the file and it does not give me that error anymore. But another."Warning: "Acitrezza.jpg" not a valid file - skipping
    Error 4: no file was specified"I tried with other files but the result is the same.This is the listing of the txt file:
    #! / Bin / bash
    FN = "Acitrezza"
    for SIZE in 29 40 48 50 57 58 60 72 75 76 80 87 100 114 120 144 152 167 180 512 1024
    do
    sips -Z $ SIZE "$ {FN} .jpg" --out "$ {FN} $ {SIZE} .jpg"
    done
    Thanks

    6 years ago

    Silvio: It looks like you have all sorts of extra spaces in your code. There shouldn't be any spaces after the $ characters and before the { brackets. There are also extras spaces in your first line too, which is probably why you couldn't get the code to run in the first place. And make sure the quotes are all straight quotes, not curly ones, though that may have happened when you pasted it in the comments field.

    Silvio
    6 years ago

    Gary: They were the quotes. Everything's ok, it works.
    Thanks 👍

Comments Closed.