Skip to content

Instantly share code, notes, and snippets.

@AndreasDickow
Created August 11, 2017 08:59
Show Gist options
  • Save AndreasDickow/c931822140a081b1be7c2f6a15b9cf02 to your computer and use it in GitHub Desktop.
Save AndreasDickow/c931822140a081b1be7c2f6a15b9cf02 to your computer and use it in GitHub Desktop.
PIL EXIF Fix for all 9 orientations
EXIF_ORIENTATION = 0x0112
def _orientation(image):
ret = {}
try:
info = image._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
print (decoded,value)
ret[decoded] = value
# if not RGB, convert
if image.mode not in ("L", "RGB"):
image = image.convert("RGB")
except:
pass#case png
if ret:
orientation = ret['Orientation']
if orientation == 2:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
image = image.rotate(180)
elif orientation == 4:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
image = image.rotate(-90, expand=1).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 6:
image = image.rotate(-90, expand=1)
elif orientation == 7:
image = image.rotate(90, expand=1).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 8:
image = image.rotate(90, expand=1)
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment