Skip to content

Instantly share code, notes, and snippets.

@sihu
Last active February 5, 2020 16:54
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 sihu/d331a6fae5c6cfe696287b0a31070d17 to your computer and use it in GitHub Desktop.
Save sihu/d331a6fae5c6cfe696287b0a31070d17 to your computer and use it in GitHub Desktop.
TensorFlow Parts
from PIL import Image, ExifTags
def rotate_image(path):
try:
image = Image.open(path)
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)
image.save(path)
print('Saving temporary image in path %s' % path)
image.close()
except (AttributeError, KeyError, IndexError):
# cases: image don't have getexif
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment