Created
March 7, 2020 10:34
-
-
Save mgd020/1cc2eb3fb6c6c660ff8afda571a698fe to your computer and use it in GitHub Desktop.
Strip info from Pillow image, correcting rotation
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
from PIL import Image | |
EXIF_ORIENTATION = 274 | |
EXIF_ORIENTATION_TRANSPOSE = [ | |
None, | |
None, | |
Image.FLIP_LEFT_RIGHT, | |
Image.ROTATE_180, | |
Image.FLIP_TOP_BOTTOM, | |
Image.TRANSPOSE, | |
Image.ROTATE_270, | |
Image.TRANSVERSE, | |
Image.ROTATE_90, | |
] | |
def strip_image(image): | |
# TODO: remove color profile? encode to sRGB? | |
exif = image.getexif() | |
image = Image()._new(image.getdata()) | |
orientation = exif.get(EXIF_ORIENTATION) | |
if orientation: | |
transpose = EXIF_ORIENTATION_TRANSPOSE[orientation] | |
if transpose is not None: | |
image = image.transpose(transpose) | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment