2024-04-25 09:56:22 发布
网友
我可以使用PIL,但是我怎样才能得到照片拍摄的日期和时间呢?
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)
我喜欢使用exif-py,因为它是纯python,不需要编译/安装,并且与python 2.x和3.x一起工作,因此非常适合与小型可移植python应用程序捆绑。
exif-py
链接: https://github.com/ianare/exif-py
获取照片拍摄日期和时间的示例:
import exifread with open('image.jpg', 'rb') as fh: tags = exifread.process_file(fh, stop_tag="EXIF DateTimeOriginal") dateTaken = tags["EXIF DateTimeOriginal"] return dateTaken
最终找到了答案,我需要的标签是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应用程序捆绑。链接: https://github.com/ianare/exif-py
获取照片拍摄日期和时间的示例:
最终找到了答案,我需要的标签是36867:
相关问题 更多 >
编程相关推荐