2017-10-13 110 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。我在原來的問題中的代碼改變我創建的圖像對象,而不是實際圖像本身的方向。

相關問題