试试这个来获取 EXIF 信息。注意:_getexif() 方法属于 JPEG 插件。它不会存在于其他类型的图像中。
import Image
from PIL.ExifTags import TAGS
im = Image.open('a-jpeg-file.jpg')
exifdict = im._getexif()
if len(exifdict):
for k in exifdict.keys():
if k in TAGS.keys():
print TAGS[k], exifdict[k]
else:
print k, exifdict[k]
对于我在硬盘上找到的随机图像,这产生了:
ExifVersion 0221
ComponentsConfiguration
ApertureValue (4312, 1707)
DateTimeOriginal 2012:07:19 17:33:37
DateTimeDigitized 2012:07:19 17:33:37
41989 35
FlashPixVersion 0100
MeteringMode 5
Flash 32
FocalLength (107, 25)
41986 0
Make Apple
Model iPad
Orientation 1
YCbCrPositioning 1
SubjectLocation (1295, 967, 699, 696)
SensingMethod 2
XResolution (72, 1)
YResolution (72, 1)
ExposureTime (1, 60)
ExposureProgram 2
ColorSpace 1
41990 0
ISOSpeedRatings 80
ResolutionUnit 2
41987 0
FNumber (12, 5)
Software 5.1.1
DateTime 2012:07:19 17:33:37
41994 0
ExifImageWidth 2592
ExifImageHeight 1936
ExifOffset 188
这是您想要的 Orientation 值。可以找到它的含义,例如在exif orientation page。
原始 exif 数据可作为来自 Image.info['exif'] 的字符串获得。
可以使用rotate() 方法完成旋转。
除了更改原始数据之外,我不知道使用 PIL 更改 EXIF 数据的方法。