Skip to content

Instantly share code, notes, and snippets.

@mgd020
Created March 7, 2020 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgd020/1cc2eb3fb6c6c660ff8afda571a698fe to your computer and use it in GitHub Desktop.
Save mgd020/1cc2eb3fb6c6c660ff8afda571a698fe to your computer and use it in GitHub Desktop.
Strip info from Pillow image, correcting rotation
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