Using convert instead of mogrify to resize images

Q When using mogrify to resize and change the format of a collection of images, how do I set a target directory for the output, and also make the name of the file contain a numeric string as a timestamp? I work with groups of young kids and things can get very busy. Often I am stalled by opening a digital photograph in Gimp, resizing it and saving it to the $HOME/.tuxpaint/saved/ directory as a PNG file so they can use it with Tux Paint. But the delay means that the other kids are left waiting. So far, my command would something look like this:

mogrify -antialias -geometry 448x376 -format png digicampic.jpg

but this does not place the finished file into $HOME/.tuxpaint/saved/, and I would also like the command to rename the file with a timestamp such as 20060719162549.png.

A Firstly, well done for getting kids working with Linux at such an early age. The more that grow up realising that Windows is one choice of several and not compulsory, the better. ImageMagick's mogrify command is for modifying images in place, so saving to another directory is out. For this, you need the convert command from the same package. This should do what you need:

for PIC in *.jpg
do
convert -antialias -resize 448x376 ${PIC}
$HOME/.tuxpaint/saved/$(date
+%Y%m%d%H%M%S).png
done

The main problem with this is that you could end up overwriting one picture with the next if they are processed within a second of each other. You could get around this by testing if a file of the same name already exists and adding an extra digit to the name if it does. But as you are using the time of conversion, not the time at which the picture was taken, you could simply pause for a second if there is a clash.

for PIC in *.jpg
do
while true
do
DEST=$HOME/.tuxpaint/saved/$(date
+%Y%m%d%H%M%S).png
[ -f ${DEST} ] || break
sleep 1
done
convert -antialias -resize 448x376 ${PIC}
${DEST} && mv ${PIC} done/
done

This version also moves the picture to another directory if the conversion is successful, so you can run the command again to process newly added images. If you want to use the time the photo was taken in the filename, replace the$(date... part of the command with

$(date -r ${PIC} +%Y%m%d%H%M%S).png

This will use the last modified time of the file for the timestamp. The date man page details the various options. A more sophisticated approach would involve reading the EXIF information from the picture. There are a number of programs for this - I prefer Exiftool (www.sno.phy.queensu.ca/~phil/exiftool).

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar