Use this shell script to batch create thumbnails from your images fast

Lets automate creating teeny tiny thumbnails out of images. Because we do not want to open every single image in Photoshop, resize it and save it as a thumbnail. Right? Right.

For the automation we will need ImageMagick. Installing it can be tough for the non-tech-savvy, however. I installed my version of ImageMagick via Homebrew, a brilliant package manager for MacOS. It highly recommended it. (You will have to find a way to install it for your specific platform, though.)

Open the terminal application and copy paste the following to install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

It will take a while to install. Once installed, you can easily install ImageMagick with Homebrew's magic. Type in the terminal:

brew install imagemagick

Congratulations. You are installing ImageMagick. You can continue with the following step while it's installing.

The script that does the magic

With this script I, hopefully, improved the experience of ImageMagick. Open up Textedit, go to the menu bar item format and click ‘Make plain text’. Copy and paste this script in there. It will use ImageMagick to create thumbnails:

#!/bin/bash
#   Read more about this script on www.geffrey.io
#
#   This script depends on having Imagemagick installed.

function askQuestion {
    echo -n "Would you like to go again? (y/n)?"
        read -r answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
        imageLoop
            else
            printf -- "\n\e[1mGood bye! Any questions? Find me on Twitter. \e[96mhttps://www.twitter.com/uxgeffrey\e[0m\n\n"
        exit;
    fi
};

function imageLoop {
read -p "Please drag and drop your folder here: "
    shopt -s extglob
    DIRECTORY="${REPLY##*( )}" 
    DIRECTORY="${REPLY%%*( )}"
    shopt -u extglob

if test -d "$DIRECTORY"
    then
        filetypes="png jpg"
        for type in $filetypes
        do
        cd "$DIRECTORY" || return
        files=$(ls *."${type}" 2>/dev/null)
            if [ ! -z "$files" ]
                then
                printf -- "\e[2mFound $type files! Making thumbnails out of them.\e[0m\n"
                for file in "$DIRECTORY"/*."${type}"
                    do
                        f=$(basename "$file" ."${type}")
                        printf --  "Making a thumbnail for \e[1m$f...\e[0m"
                        magick convert "$file" -thumbnail x20 "$DIRECTORY"/"$f"-thumb."${type}"
                        printf -- '\033[32m done!\033[0m\n'
                    done
            else
                printf -- "Did not find any $type. Checking for other files...\n";
            fi
        done
                printf -- "\n\e[1;36mYour thumbnails have been made!\nYou can find them in the same folder as your images.\e[0m\n\n";
                askQuestion
else
    printf -- "\n\e[91mThis seems not to be a directory. Please drag and drop a directory of pictures in the terminal window.\e[0m\n\n"
    askQuestion
fi
};

imageLoop;

Save it as make-thumbnails.command in any location. You will now need to make this script executable. You want it to open on double-click, of course. Find the terminal application again and type the following. Do not hit enter yet!

chmod 755

Now drag and drop your make-thumbnails.command file inside the terminal. It should look something like this:

chmod 755 /Users/geffrey/Desktop/make-thumbnails.command
Press enter. That's it. You can now double click the script file. It will open a window which instructs you to drag and drop your folder of images. Do that, press enter and voilà . Thumbnails galore.

Changing the size of the thumbnails

The resulting thumbnails will be 20 pixels in height. You might want to change that number to a bit higher. Or even change the size completely.

In order to do this you will need to change one simple line in the script. Find

magick convert "$file" -thumbnail x20 "$DIRECTORY"/"$f"-thumb."${type}"
and replace the x20 with whatever you want. (The 'x' before the number means width=proportional, height=20. The x after the number means vice versa.)

Be wary, putting for example 720x1080 will try to FIT the images inside that box. If you want to resize the thumbnails regardless of aspect ratio, add an \! flag. Meaning 720x1080\! will ignore the aspect ratio and squeeze the image into that size.

Do reach out to me on Twitter, or other social media, when you have any questions. I will do my best to help you.