Skip to content

Instantly share code, notes, and snippets.

@vighneshbirodkar
Created April 4, 2015 17:57
Show Gist options
  • Save vighneshbirodkar/5b0b3c84083c34963ee8 to your computer and use it in GitHub Desktop.
Save vighneshbirodkar/5b0b3c84083c34963ee8 to your computer and use it in GitHub Desktop.
Python snippet to generate captions based on EXIF data
from PIL import Image, ImageDraw, ImageFont
import os
from PIL.ExifTags import TAGS
# get an image
fnt = ImageFont.truetype('Ubuntu-R.ttf', 30)
for name in os.listdir('images'):
print name
img = Image.open('images/' + name)
exif = {
TAGS[k]: v
for k, v in img._getexif().items()
if k in TAGS
}
del exif['MakerNote']
info_string = ''
info_string += 'A = f/' + str(float(exif['FNumber'][0])/exif['FNumber'][1])
info_string += ', SS = 1/' + str((exif['ExposureTime'][1])/exif['ExposureTime'][0]) + 's'
info_string += ', ISO = ' + str(exif['ISOSpeedRatings'])
print info_string
base = img.resize((800, 600), Image.ANTIALIAS).convert('RGBA')
txt = Image.new('RGBA', base.size, (255,255,255,0))
d = ImageDraw.Draw(txt)
d.rectangle((340,570,800,600), fill=(0,0,0, 200))
d.text((350,570), info_string, font=fnt, fill=(255,255,255,255))
out = Image.alpha_composite(base, txt)
out.save('output/' + name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment