Tag Archives: image

Bulk Image Resizing in Linux

This is even easier than adding a watermark! If a directory is filled with, say, 30 PNG images and you want them all resized so that they’re all the same width (let’s say 600 pixels wide), then all you do is:

cd /name/of/directory/with/original/images
mkdir resizedones
mogrify -path ./resizedones -resize 600 *.png

It doesn’t get much easier than that! Mogrify is another of the utilities provided by Imagemagick and it works on any image format Imagemagick itself supports (so doing the above with a final *.jpg will work just as well …assuming your source images are actually JPEGs, of course!) If you wanted the originals replaced by the resizing (rather than, as above, being written out to a new directory), just lose the -path bit. This command, in other words, does an “in place” resizing, such that the originals are irretrievably lost:

cd /name/of/directory/with/original/images
mkdir resizedones
mogrify -resize 600 *.png

The mogrify commands takes loads more arguments and can be used to do stacks more transformations, but that’s all I need it for right now!

Bulk Watermarking of Images in Linux

Here’s a simple requirement: take a directory of 30 or so screen captures (and therefore all about the same size -in this case, 600×499 pixels) and slap a copyright notice on them. Painful fiddling multiple times if you’re using a GUI image editor (like GIMP) -and you’ll likely end up with the notice in slightly different places on each image, thanks to the ‘hand drawn’ nature of the process. But it all becomes a doddle if you’ve got access to Imagemagick’s convert function! It’s do-able with the following simple command (all on one line):

convert -pointsize 10 -fill grey80 -draw 'text 240,485 "Copyright © 2010 Diznix.com"' screenshot01.png screenshot01.png

The basic command is convert.

The pointsize switch says how big the text of the ‘watermark’ will be.

The fill grey80 switch specifies the colour of the text that will be placed on the image. Grey80 is a very light shade of grey; grey10 is practically solid black. A complete list of colour names that can be used here can be obtained by issuing the command showrgb.

The draw switch takes a single-quoted argument that says what text will be drawn on the image. Within the pair of single quotes, you get three different elements. First, you say you’re drawing text. Next, you say where the text should be added to the image, counting from the left edge rightwards and top edge downwards, measured in pixels. So, my text in this example will be written 240 pixels in from the left, and 485 pixels down from the top. Since my image is 600 pixels wide and 499 pixels tall, my text will be written in the middle-ish of the very bottom of the picture. Finally, you write the text you actually want added to the image, enclosed in double quotation marks,

Two file name arguments complete the command. The first is the ‘input image’ name, the second is the ‘output image’ name. If the two are the same, as they are here, you’ll end up adding the text to the source image (a process which cannot therefore be reversed). If the second name is different from the first, you get the image-plus-text output as a new file, leaving the text-less original untouched.

And here’s the outcome of all of that. This is the original:

And here’s the processed version, with a discrete copyright ‘watermark’ added:

Job done! Not only is it relatively simple to do, it’s precise, to the pixel, as to where the text notice is placed on the image. So, repeat as many times as you like on different images, and so long as that “240,485″ bit (in my example) doesn’t change, the notice will always end up in exactly the right spot, image after image. All that remains is to wrap the thing up into a bash script loop so that all 30 images can be processed in one hit. The following is a trivial way of doing that, neither refined nor particularly original:

#!/bin/bash
echo -e "All images in this directory are about to be over-written."n"Are you sure want to continue? {Y/n}"
read REPLY
if
  [[ $REPLY = "Y" || $REPLY = "y" ]]; then
  file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE
    do
      echo Watermarking $IMAGE
      convert -pointsize 10 -fill grey80 -draw 'text 240,485 "Copyright © 2010 Diznix.com"' "$IMAGE" "$IMAGE"
    done
else
  echo exiting
  exit 0
fi
exit 0

Save that somewhere appropriate (like /usr/bin) with a name like watermarkme.sh and change its permissions to allow execution (chmod 775 /usr/bin/watermarkme.sh, for example), and then you can invoke the script at will:

cd /home/hjr/Desktop/screengrabs
/usr/bin/watermarkme.sh

Thank heavens for Imagemagick (and the command line!)