Reputation: 207
My code is:
from PIL.ExifTags import *
from PIL import Image
import sys
import os
import glob
import time
image_fileList = []
mainFolder = 'C:' + chr(92) + 'Users' + chr(92) + 'aa\Desktop\ToDigitalFrame\To select from'
folderList = [x[0] for x in os.walk(mainFolder)]
print(folderList)
def saveImage(imgName):
imgName.save('rotated.jpg')
for folder in folderList:
print(folder)
for image_file in glob.glob(folder + '/*.jpg'):
print(image_file)
if not os.path.isfile(image_file):
sys.exit("%s is not a valid image file!")
img = Image.open(image_file)
info = img._getexif()
exif_data = {}
if info:
for (tag, value) in info.items():
decoded = TAGS.get(tag, tag)
if type(value) is bytes:
try:
exif_data[decoded] = value.decode("utf-8")
except:
pass
else:
exif_data[decoded] = value
else:
sys.exit("No EXIF data found!")
print(exif_data)
if exif_data['Orientation'] == 6:
im = Image.open(image_file)
im.rotate(280, expand=True).show()
# saveImage(im)
im.save('rotated.jpg')
elif exif_data['Orientation'] == 3:
im = Image.open(image_file)
im.rotate(180, expand=True).show()
saveImage(im)
elif exif_data['Orientation'] == 8:
im = Image.open(image_file)
im.rotate(90, expand=True).show()
saveImage(im)
elif exif_data['Orientation'] == 4:
im = Image.open(image_file)
im.rotate(270, expand=True).show()
saveImage(im)
In my pictures, the Orientation is mostly 6 (6 = Rotate 90 CW)I want to rotate them with 270 degrees.
So, my preview is:
And my saved output file is equal with the default file:
So, it doesn't really save the rotated picture, this code just saves the original picture one more time. I want to save the rotated picture! I know I'm rotating the picture with 280 degrees instead of 270 degrees but, just to show it doesn't save it.
Upvotes: 3
Views: 7874
Reputation: 131
the Image.rotate() returns a rotated copy of this image.
so how about try:
im = Image.open(image_file)
im=im.rotate(270, expand=True)
im.show()
im.save('rotated.jpg')
see the docs:https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate
Upvotes: 5