A week ago I set up a Tumblr site with the idea I’d post photos on there, hopefully at least once a day. Sort of a photo-based microblog. After a week of posting stuff I realized it would be really easy to set something like this up on my own site. It works like this:
- Email myself a photo where the subject line has a keyword in it that procmail recognizes, sending the email off to a Python script.
- The Python script processes the email, extracting the photo name and caption from the text portion of the email, rotates and resizes the photo, and saves it in a place accessable from my web server. The photo metadata (size, when the photo was taken, caption and path) is stored in a database.
- A Django app generates the web page from the data in the database.
There are a few tricky bits here. First is handling the rotation of the photos. At least with my phone, the image data is always stored in landscape format, but there’s an EXIF tag that indicates how the data should be rotated for display. So I read that tag and rotate appropriately, using the Python Imaging Library (PIL):
import StringIO import Image import ExifTags orientation_key = 274 if orientation_key in exif: orientation = exif[orientation_key] if orientation == 3: image_data = image_data.rotate(180, expand = True) elif orientation == 6: image_data = image_data.rotate(270, expand = True) elif orientation == 8: image_data = image_data.rotate(90, expand = True)
For simplicity, I hard coded the orientation_key above, but it’s probably better to get the value from the ExifTags library. That can be done using this list comprehension:
orientation_key = [key for key, value in \ ExifTags.TAGS.iteritems() if value == 'Orientation']
Resizing the image is relatively easy:
(x, y) = image_data.size if x > y: if x > 600: image_600 = image_data.resize( (600, int(round(600 / float(x) * y))), Image.ANTIALIAS ) else: image_600 = image_data else: if y > 600: image_600 = image_data.resize( (int(round(600 / float(y) * x)), 600), Image.ANTIALIAS ) else: image_600 = image_data
And the whole thing is wrapped up in the code that parses the pieces of the email message:
import email msg = email.message_from_file(sys.stdin) headers = msg.items() body =  for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue content_type = part.get_content_type() if content_type == "image/jpeg": image_data = Image.open(StringIO.StringIO( part.get_payload(decode = True) )) elif content_type == "text/plain": charset = get_charset(part, get_charset(msg)) text = unicode(part.get_payload(decode = True), charset, "replace") body.append(text) body = u"\n".join(body).strip()
The get_charset function is:
def get_charset(message, default="ascii"): """Get the message charset""" if message.get_content_charset(): return message.get_content_charset() if message.get_charset(): return message.get_charset() return default
Once these pieces are wrapped together, called via procmail, and integrated into Django, it looks like this: photolog. There’s also a link to it in the upper right sidebar of this blog if you’re viewing this page in a desktop web browser.