Resize / Convert multiple images with ImageMagick

Resizing and converting multiple images at once.

Resizing and converting multiple images at once.

Recently I was faced with the task of creating thumbnails for a bunch of images. So fearing that I would have to go through each one in Gimp and resize it manually I wrote a small bash script which uses ImageMagick to make the actual conversion. Now, I made it a bit more “user friendly” and have posted it here in case you need to resize multiple images at once.

You can use and modify the script as you find suitable. It looks more complicated than it actually is, as the actual conversion code (using ImageMagick) is the last block of code at the end. The rest is just gathering information from the user in one of two ways:

  • Using command-line arguments, where if you don’t supply all of them the rest will be defaulted.

e.g.  imgResizer -l /the/image/dir/path -s 300 -i jpg -p new_

  • Or simply calling the script without any arguments, in which case the script will prompt the user for the required info.

e.g. imgResizer


To use it you need to have imagemagick installed (if you prefer using a different program it could easily be modified). To get imagemagick, open terminal and say:

sudo apt-get install imagemagick

Now get my script by copy-pasting the code into an empty text file (call it, for example, imgResizer, without extension)

you might have to make it executable by typing the following into your terminal (provided that you have navigated to your script location)

sudo chmod 744 imgResizer

in order to access it from any location you need to put it in /usr/local/bin (at least on Ubuntu 11.04 but also most linux distros)

sudo mv imgResizer /usr/local/bin

Now your good to go, so navigate to your folder with all the images you want to resize

cd /folder/with/lots/of/images

and if you want to create thumbnails like me you would say something like that:

imgResizer  -s 125 -i jpg -o png -p th_

which would resize all jpg’s in that folder (uniformly) to a width of 125px and covert them to png’s and add a “th_” to the beginning of the new image names ( image001.jpg -> th_image001.png ), keeping the originals intact.

Note that I intentionally missed out the -l which is used to specify the location of the images, but since I didn’t provide a path it assumes the current working directory of the terminal, using pwd (print working directory).

# Resize multiple images script using Imagemagick


# Check for command-line arguments
# All Arguments are optional (e.g. if non are supplied, the script will prompt user)
# -l  LOC - location is optional, if left blank, pwd will be assumed
# -s  SIZE - is necessary if you don't want to be asked by the script later
# -i  IN_EXT - is necessary if you don't want to be asked by the script later
# -o  OUT_EXT - is optional, if left out, the same extension will be assumed and no conversion will take place (only resize)
while getopts "l:s:i:o:p:" opts; do
	case ${opts} in
		  LOC=${OPTARG} ;;
		  SIZE=${OPTARG} ;;
		  IN_EXT=${OPTARG} ;;
		  OUT_EXT=${OPTARG} ;;
		  PREF=${OPTARG} ;;

# Add correct defaults if arguments are missing
if [ -n "$1" ]; then
	if [ -z "$LOC" ]; then

	if [ -z "$SIZE" ]; then

	if [ -z "IN_EXT" ]; then

	if [ -z "$OUT_EXT" ]; then

	if [ -z "$PREF" ]; then

# Check if there are any cmd-line args
elif [ -z "$1" ]; then

	# Get the location of the images that need to be converted
	echo "Type in the full directory of the folder with the images you want to resize or press [Enter] if they are in the current directory."
	read LOC

	if [ -z "$LOC" ]; then

	cd $LOC

	# Get the width of the new desired size (it will resize uniformly)
	echo "Type in the image width you want and press [ENTER]: "
	read SIZE

	# Get the extension type of the images that will be manipulated
	echo "Type the INPUT extension type (e.g. png) and press [ENTER]:"
	read IN_EXT

	# Get the extension type (if different) for the output images
	echo "Type the OUTPUT extension type (e.g png) and press [ENTER] or just [ENTER] to use the same extension as the input"
	read OUT_EXT

	if [ -z "$OUT_EXT" ]; then

	# Get the prefix to the new images if such is required so that the originals are not overwritten (Not necessary if changing to a different format)
	echo "Type the prefix to the new set of images (eg. new_) and press [ENTER]"
	read PREF

# Perform the actual conversion with imagemagick
for IMG in *.$IN_EXT
	echo converting $NAME to $PREF$NEW_NAME.$OUT_EXT
	convert $NAME -resize $SIZE $PREF$NEW_NAME.$OUT_EXT

Apparently you can achieve similar results without the need for any script by simply going to into your directory and running imagemagick with a command like that:

convert -resize 720 *.png

which will resize all the png’s in that directory, uniformly, to a width of 720px. They however will bare the same name as the originals, and you have just a bit more limitations than what the script provides.

Nevertheless, why do something the easy way, when you can do it the hard way!? 0_o


Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: