我可以使用PIL来get the EXIF data from an image,但是如何获取拍摄照片的日期和时间?
最终找到答案,我需要的标签是36867:
from PIL import Image
def get_date_taken(path):
return Image.open(path)._getexif()[36867]
[我喜欢使用exif-py
,因为它是纯python,不需要编译/安装,并且可以与python 2.x和3.x一起使用,因此非常适合与小型便携式python应用程序捆绑在一起。
try:
import PIL
import PIL.Image as PILimage
from PIL import ImageDraw, ImageFont, ImageEnhance
from PIL.ExifTags import TAGS, GPSTAGS
except ImportError as err:
exit(err)
class Worker(object):
def __init__(self, img):
self.img = img
self.get_exif_data()
self.date =self.get_date_time()
super(Worker, self).__init__()
def get_exif_data(self):
exif_data = {}
info = self.img._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_data = {}
for t in value:
sub_decoded = GPSTAGS.get(t, t)
gps_data[sub_decoded] = value[t]
exif_data[decoded] = gps_data
else:
exif_data[decoded] = value
self.exif_data = exif_data
# return exif_data
def get_date_time(self):
if 'DateTime' in self.exif_data:
date_and_time = self.exif_data['DateTime']
return date_and_time
def main():
date = image.date
print(date)
if __name__ == '__main__':
try:
img = PILimage.open(path + filename)
image = Worker(img)
date = image.date
print(date)
except Exception as e:
print(e)
[在最新版本的Pillow(我相信6.0+以上)中,这已稍有改变。
从_getexif()使用键'DateTimeOriginal'返回的字典中?