Just like the title says I m trying to Load an image file into a numpy array with Exif orientation support. I m doing this to Prevents upside-down and sideways images for face_recognition as it does not work on pictures taking by Iphones. To fix that I m using this script:
import matplotlib.pyplot as plt
import image_to_numpy
img = image_to_numpy.load_image_file("my_file.jpg")
plt.imshow(img)
plt.show()
This works for one image but I want to create a loop to do this with multiple images in a folder and save it in a different folder. Any idea on how to do that ?
Are you talking about something like this. I m pretty sure I m missing something in this below:
import glob
import os
for i in range(10)
filepath = glob.glob(os.path.join('path/images/', *.jpg))
try:
image=Image.open(filepath)
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(filepath)
image.close()
except (AttributeError, KeyError, IndexError):
# cases: image don't have getexif
pass ```