Last active
February 5, 2020 16:54
-
-
Save sihu/d331a6fae5c6cfe696287b0a31070d17 to your computer and use it in GitHub Desktop.
TensorFlow Parts
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, 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