Resize, Standarize Canvas and Watermark Images with ImageMagick

This post is the result of lots of swearing and wasted time waiting for photoshop to resize several thousand files, I still can’t believe how fast this task was using Imagemagick.

Not only have i resized &  standardised the canvas size on over 2000 images, Ive also managed to automatically watermark them all with just a few linux commands.

Problem : I have a load of images on my ecommerce shop (over 3000 + thumbs), the source images are in all kinds of sizes so we need to standardise, basically i need to

– resize all to max fit inside 500×500 canvas and preserver aspect ratio, use white background if padding required
– add watermarks to every images
– create 150×150 thumbs

Having spent hours trying to automate this with photoshop cs3 on windows I hit a few problems:

– no standard way to fix canvas size
– resizing this many images with actions (or batch mode in fireworks) takes AGES
– no standard way to add watermarks apart from photoshop actions, again hours to run on that many image.

Solution : Imagemagick and a few minutes!

So to the rescue pops my good friend Tombott with his recommendation to try imagemagick under Linux. WOW!!! Here is how i solved my problem in literally a couple of commands (at this point i moved to my Linux machine to achieve this):

a) Deleted all the thumbs – just remove any thumbs we will recreate them later.
b) Resized all large images to 500×500 and preserved aspect ratio using white background where required. Make a copy of your source images in a new folder (i called it large/). cd into that folder then run this:

mogrify -resize 500x500 -background white -gravity center -extent 500x500 -format jpg -quality 75 *.jpg

Note: I found this command resizes perfectly but leaves some images on a non 500×500 canvas, I think this is because some of the source images were already smaller than 500×500, to standardise canvas just run this (which is basically the command without the resize):

mogrify -background white -gravity center -extent 500x500 -format jpg -quality 100 *.jpg

c) Add watermark to all images.

I create a 500×500 image in photoshop/gimp/whatever you want, with no layers apart from your watermark text, i just used my domain name with an outer glow on the text to make it harder to remove from photos and save it in your images folder as watermark.png (make sure you save it as a png file with transparency ON otherwise it will just overwrite your photos). Then make sure you are inside the folder you create in step b (source/large/) in my case, then run this:

find . -name "*jpg" -type f -exec composite -gravity south -quality 100 -dissolve 100 watermark.png {} {} \;

Note: I leave quality on 100 and dissolve (transparancy) on 100 but you can play with this if you need to.

d) Create your thumbs.

Using mogrify again I now created nice standard 100×100 thumbnails. So navigate into your large/ folder (or whatever you called it) and run this:

mogrify -resize 100x100 -background white -gravity center -extent 100x100 -format jpg -quality 75 -path ../thumbs *.jpg

That’s it! Over 3000 images processed in under 5 minutes, result!

You should now end up with these folders:

source/ – you original source files
source/large – perfect 500×500 images with watermark
source/thumbs – perfect 100×100 images with watermark

Imagemagick is my new favourite toy, if you want to check out what else it can do visit the imagemagick website.

Note: Imagemagik run very well under windows too which is where i first tried it, however i did find that the ‘find’ command came in invaluable when apply batch operations where the filename was required in the command (e.g. the watermarketing requires input and output filename to be used). I’ve not worked out a way to do this on windows yet although im sure someone could write a little script to do it.

Addition – Quick Resize of files in entire folder tree (added 11/09/13)

To do a simple resize on a folder and all its subfolders (this wont standardise canvas sizes but is useful for quickly lowering the filesize of a folder containing files and sub folders also with files:

find /your/folder/path/ -type f -name “*.jpg” -exec mogrify -verbose -resize 500×500 -quality 75 {} \;

This will resize all jpg files (obviously change this if you want to handle other file formats) to 500×500 quality 75% in the folder and all sub folders.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s