Saturday, December 15, 2012

Annotate a JPG with its EXIF date and time

If you need to annotate a bunch of JPG pictures the following simple bash script could come in handy. Script uses pictures EXIF date and time information but could be easily patched to include any other text. The only dependencies script has are Image Magic and awk:
alex@xubuntu:~$ cat bin/ 

annotSize=32 # 1/32 of a picture height

[ -n "$1" ] && indir=$1

# shopt is needed to discard *.jpg (or *.JPG) filename in 
# the for loop if there are no files with that extentions
shopt -s nullglob

for i in ${indir}/*.{jpg,JPG} 
  dim=$(identify -format "%w %h" $i)
  width=${dim%% *}
  height=${dim#* }
  echo "$i"
  convert $i -font "$font" -fill "$fontColor" \
          -pointsize $(awk "BEGIN{ print (1.2*$height)/$annotSize }") \
          -gravity SouthEast -annotate 0 "%[exif:DateTimeOriginal]" \

Script's input parameter is a directory with pictures (default value is /tmp). Annotation size is specified as a fraction of the picture height. Which means that annotation size is automatically adjusted to the picture size. Fonts available to the ImageMagic could be listed with the 'convert -list font' command.

No comments: