Nifty tidbits
Nifty tidbits and random thoughts on technology and anything else that catches my fancy
PIL vs Imagemagick
Decided that I want to timestamp my photo collection with the date from the exif data. Many digicams have an option to do this – unfortunately, my Panasonic DMC-LZ8 doesn’t seem to do this. I knew imagemagick would do the trick, but thought it would be a good time to play around with PIL and python.
Here’s my PIL effort – functional, but one that came with quite some amount of googling and trying to make sense of the PIL documentation which is inadequate at best.
from PIL import Image
from PIL import ImageFont, ImageDraw
from PIL.ExifTags import TAGS
from os.path import basename, dirname,join
import logging
import sys
import datetime
import time
# Important: I set out to write the image annotation in PIL - there's one serious drawback though. When saving
# the image, the exif data is'nt preserved.
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger()
logger.level = logging.DEBUG
def readExif(image):
info = image._getexif()
ret ={}
for tag,value in info.items():
ret[TAGS.get(tag,tag)] = value
dt = datetime.datetime (*time.strptime (ret['DateTime'],"%Y:%m:%d %H:%M:%S")[0:6])
ret['DateTime'] = dt
return ret
def annotateImage (file):
i = Image.open(file)
font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", 36)
exif = readExif(i)
draw = ImageDraw.Draw(i)
width, height = i.size
draw.text((width * 0.7, height - 100),exif['DateTime'].strftime("%a %d-%b-%Y %l:%M %p"), font=font, fill='orange')
outfile = join(dirname(file), "Ann_" + basename(file))
i.save (outfile, quality=98)
logger.debug (outfile + " saved")
if __name__== "__main__":
logger.debug ("getting exif for " + sys.argv[1])
for file in sys.argv[1:]:
logger.debug ("Annotating " + file)
annotateImage(file)
Unfortunately, PIL has a fatal flaw – you can annotate the image and save it – but then the saved image doesn’t retain the original image’s exif metadata. I also tried the exiv2 library, but couldnt figure out a way to load the image, annotate it and then copy over the metadata. Googling around didn’t turn up any intersting solutions – so if any of you have any ideas, please share.
Meanwhile, as I was getting tired of coaxing PIL to do what I want, I just wrote a a little bash script to do the same in imagemagick. Its as painless as it can be, comes with excellent documentation, hardly any gotchas, a world of options in case you feel creative and the job gets done in 10 mins. Here’s the bash script below.
#! /bin/bash
# script adds a black 18px bottom border to the pic with the Exif datetime tag
# no safety checks
. Original pics are left untouched.
while [ "x$*" != "x" ]
do
file=$1;
shift;
outfile="$(dirname "$file")/Ann_$(basename "$file")"
echo $outfile
echo $file
date=$(identify -verbose "$file" | grep 'DateTime:'| sed 's/ Exif:DateTime: //;s/:/-/;s/:/-/')
date="$(date -d "$date" +"%a %d-%b-%Y %l:%M %p")"
convert "$file" -size 1x18 xc:Black -fill White -background Black -append -gravity Southeast -draw "text 0,0 '$date'" "$outfile"
done
Overall, the experience left me dissappointed and dissatisfied with PIL.


Pingback: Nex blog » Shared Items - January 25, 2009
Pingback: Nex blog » Links del giorno: January 25, 2009
Just passing by.Btw, you website have great content!
______________________________
Don’t pay for your electricity any longer…
Instead, the power company will pay YOU!
Pingback: 用PIL画天气预报 | rox