Last active
February 3, 2019 20:31
-
-
Save AlexRiina/db6c8be0b41db44b95f8964b1f6057a1 to your computer and use it in GitHub Desktop.
Overlay photo speed, aperture, and ISO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Add legend with exposure speed, aperture, and ISO to top left of jpeg photo exif data. | |
Copies original exif data into output file. | |
Requires Python3.6, and some version of Pillow | |
""" | |
import argparse | |
from PIL import Image, ImageDraw, ImageColor, ImageFont | |
from PIL.ExifTags import TAGS, GPSTAGS | |
def parse_speed(exif_speed): | |
assert exif_speed[0] == 1 | |
return f"{exif_speed[0]}/{exif_speed[1]}" | |
def parse_aperture(exif_aperture): | |
assert exif_aperture[1] == 10 | |
return f"f{exif_aperture[0]/exif_aperture[1]}" | |
def parse_iso(exif_iso): | |
return f"ISO {exif_iso}" | |
def main(in_file, out_file, comment=None): | |
img = Image.open(in_file) | |
exif = img._getexif() | |
exif_data = { | |
TAGS[tag] or GPSTAGS[tag]: value | |
for tag, value in exif.items() | |
if tag in TAGS or tag in GPSTAGS} | |
assert exif_data['ExifVersion'] == b'0230' | |
offset = min(img.height, img.width) // 20 | |
ImageDraw.Draw(img).multiline_text( | |
(offset, offset), | |
'\n'.join(filter(None, [ | |
comment, | |
parse_speed(exif_data['ExposureTime']), | |
parse_aperture(exif_data['FNumber']), | |
parse_iso(exif_data['ISOSpeedRatings']), | |
])), | |
ImageColor.getcolor("white", img.mode), | |
font=ImageFont.truetype("arial.ttf", offset), | |
) | |
# exif takes a bytes object and not the dictionary from _getexif() | |
img.save(out_file, "JPEG", exif=img.info['exif']) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(__doc__) | |
parser.add_argument("input_file", type=str) | |
parser.add_argument("output_file", type=str) | |
parser.add_argument("-c", "--comment", required=False, type=str) | |
args = parser.parse_args() | |
main( | |
args.input_file, | |
args.output_file, | |
args.comment, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment