2017-10-13 113 views
2

当我在python中做Image.open时,它有时会翻转图像的宽度和高度。经过一番研究(看这post的解释),似乎如果图像有Exif Orientation metadata与它关联,那么这将导致尊重该属性的应用程序旋转它。手动设置Exif属性并在Python中保持图像的原始宽度和高度

所以首先我做

identify -verbose image.JPG | grep Orientation 

它返回,这意味着图像具有财产,因此将被翻转测试我的图像的Exif属性。如果响应是那么图像没有Exif Orientation metadata,因此不翻转。由于我不想翻转图像,我尝试通过从post采取建议手动设置Exif property

所以我尝试在我的代码中手动设置orientation.exif.primary.Orientation[0] = 1。就像这样:

from PIL import Image 
import pexif 


orientation = pexif.JpegFile.fromFile('image.JPG') 

print("BEFORE:") 
print(orientation.exif.primary.Orientation[0]) 

orientation.exif.primary.Orientation[0] = 1 

print("AFTER:") 
print(orientation.exif.primary.Orientation[0]) 


img = Image.open('image.JPG') 
print(img.size) 

这纠正打印 后,但是经过,它实际上并没有将其设置为1在现实生活中,因为当我再次运行identify -verbose image.JPG | grep Orientation,但仍呈现。那么我如何真正解决这个问题,而不是让图像的宽度和高度翻转?

回答

0

我不认为这一点。这从超级用户post解决了我的问题。

修复:

import os 
os.system("exiftool -Orientation=1 -n image.JPG") 

这将实际图像的方向为1。我在原来的问题中的代码改变我创建的图像对象,而不是实际图像本身的方向。

相关问题