PIL - Images not rotating

☆樱花仙子☆ 提交于 2020-06-27 17:42:15

问题


I'm curious as to why my image is not rotating, it ends up in the same position every time.

img = Image.open(r'C:\Users\Brett\Downloads\testing.jpg')
exif_data = {
    TAGS[k]: v
    for k, v in img._getexif().items()
    if k in TAGS
}
print(exif_data['Orientation'])

That outputs a '6'

No matter how many degrees I tell the image to rotate it ends up in the same position.

if exif_data['Orientation'] == 6:
    img.rotate(90)

or

if exif_data['Orientation'] == 6:
    img.rotate(270) 

or

if exif_data['Orientation'] == 6:
    img.rotate(180)

I always end up with an image rotated 90 degrees counter-clockwise. Am I doing something obviously wrong?


回答1:


From the (DOCS)

Image.rotate(angle, resample=0, expand=0, center=None, translate=None)

Returns a rotated copy of this image. This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.

The image is not rotated in place. You need to store the image returned from rotate(). Maybe something like:

if exif_data['Orientation'] == 6:
    new_image = img.rotate(180)


来源:https://stackoverflow.com/questions/43966248/pil-images-not-rotating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!