From f297c244a478506724ee0723ccd88b3daba4a0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Midb=C3=B8e?= Date: Mon, 15 Apr 2019 22:05:06 +0200 Subject: [PATCH] Support for automatic rotating images based on exif metadata. --- processor.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/processor.py b/processor.py index bd750f5..53a5a28 100644 --- a/processor.py +++ b/processor.py @@ -26,6 +26,7 @@ def processImage(file, outputPath=None): print('outputpath', outputPath) image = Image.open(file) + image = rotateFromExifMetadata(image) fileID = uuid.uuid4().hex for size in OUTPUT_SIZES: @@ -45,6 +46,21 @@ def processImage(file, outputPath=None): 'variations': list(map(lambda vairation: vairation['name'], OUTPUT_SIZES)) } +def rotateFromExifMetadata(image): + for orientation in ExifTags.TAGS.keys(): + if ExifTags.TAGS[orientation]=='Orientation': + break + exif=dict(image._getexif().items()) + + if exif[orientation] == 3: + image=image.rotate(180, expand=True) + elif exif[orientation] == 6: + image=image.rotate(270, expand=True) + elif exif[orientation] == 8: + image=image.rotate(90, expand=True) + + return image + def generateFilename(fileID, modifier, outputPath): filename = "{}_{}.{}".format(fileID, modifier, OUTPUT_EXTENSION) return os.path.join(outputPath, filename)