From abfaf27cee03643d2c8744a15589868cb55afdf9 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 15 Jul 2022 21:36:12 -0700 Subject: [PATCH] Add exifread function. --- voussoirkit/imagetools.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py index 21b8654..44ab24b 100644 --- a/voussoirkit/imagetools.py +++ b/voussoirkit/imagetools.py @@ -1,6 +1,7 @@ import copy import datetime import dateutil.parser +import exifread import io import PIL.ExifTags import PIL.Image @@ -82,12 +83,25 @@ def get_exif_datetime(image) -> datetime.datetime: if key in PIL.ExifTags.TAGS } exif_date = exif.get('DateTimeOriginal') or exif.get('DateTime') or exif.get('DateTimeDigitized') + if not exif_date: - return + return None exif_date = re.sub(r'(\d\d\d\d):(\d\d):(\d\d)', r'\1-\2-\3', exif_date) return dateutil.parser.parse(exif_date) +def exifread(path) -> dict: + if isinstance(path, PIL.Image.Image): + handle = io.BytesIO() + path.save(handle, format='JPEG', exif=path.getexif(), quality=10) + handle.seek(0) + elif isinstance(path, pathclass.Path): + handle = path.open('rb') + elif isinstance(path, str): + handle = open(path, 'rb') + + return exifread.process_file(handle) + def pad_to_square(image, background_color=None) -> PIL.Image: ''' If the given image is not already square, return a new, square image with