Linux Directory Batch .jpg Image Resizing and Watermarking

Submitted by nigel on Monday 10th May 2010

These days whenever I take digital pictures I always make sure the image quality and image size settings are set to maximum. It makes sense since hard disk storage is cheap, and processors have the power to crunch large files under Photoshop or Gimp.

However, the problems arrive when I want to upload the images to Photobucket for my friends to access. The saved images are usually above 6MB which is a ridiculous size to attempt to upload to these free image hosting services.

So, a script is required to resize all the images in a directory to take the legwork out of the process. To do this, use the Linux convert command with syntax like:

convert infile -resize 600 -quality 80 outfile

resize: resizes the image to a width of 600px. Change this to what you need. 600 may be too low for you;
quality: The quality of the image. By using the setting of say 80, you will vastly reduce the size of the files.

This now needs to be wrapped into a command for the command line. I have copied all my photographs from a weekend at Wembley stadium to the directory 2010-05-08 Wembley. Note how I always prefix an advent with the date so it is easy in chronological order. I then create another directory with the prefix of -Processed to indicate these are the files that have been resized.

badzilla@laptop4:~/Pictures/2010-05-08 Wembley> mkdir "../2010-05-08 Wembley-Processed"
badzilla@laptop4:~/Pictures/2010-05-08 Wembley> for k in `ls *.jpg`; do convert $k -resize 600 -quality 80 "../2010-05-08 Wembley-Processed/$k"; done
badzilla@laptop4:~/Pictures/2010-05-08 Wembley>
Done! All photos are now a much more respectable ~80k instead of 6MB! However, in this day and age, it is regrettably all too often that pictures are stolen from websites and reused elsewhere. To combat this, it is useful to watermark all photos. Thankfully, there is a very handy Linux command line utility called composite that can do just that. So lets create a new directory for the watermarked images, and issue the composite command.
badzilla@laptop4:~/Pictures/2010-05-08 Wembley-Processed> mkdir "../2010-05-08 Wembley-Processed-Watermarked"
badzilla@laptop4:~/Pictures/2010-05-08 Wembley-Processed> for k in `ls *.jpg` ; do composite /usr/local/badzilla/resources/Transparency600x450.png $k "../2010-05-08 Wembley-Processed-Watermarked/$k"; done
badzilla@laptop4:~/Pictures/2010-05-08 Wembley-Processed>

In reference to the above sequence, note how I had previously created a PNG watermark called Transparency600x450.png to coincide with the size of the photos. This doesn't necessarily have to be the case - a transparency can be overlaid using an offset in pixels from the top left corner. I used Photoshop CS2 to create the PNG file - and set the opacity to 50% of the layer containing the logo.

This entire process obviously lends itself to shell scripting. A very basic script is shown below. It would be smarter to take a command line argument, rather than set the variable in the code, but I'll do that next time!

# script to crop and watermark
dir="/home/badzilla/Pictures/2010-08-08 Yorkshire Dales English Lakes Vehicle Day"
cd "$dir"
for k in `ls *.JPG`; 
    convert $k -resize 600 -quality 80 "$dir-$crop/$k"; 
cd "$dir-$crop"
for k in `ls *.JPG` ; 
    composite /usr/local/badzilla/resources/Transparency600x450.png $k "$dir-$crop-$water/$k"; 
blog terms