Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.