Support for automatic rotating images based on exif metadata.

This commit is contained in:
Kevin Midbøe
2019-04-15 22:05:06 +02:00
parent 99618b884e
commit f297c244a4

View File

@@ -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)