Skip to main content
 首页 » 编程设计

Python EXIF 无法找到拍摄日期信息,但在通过 Windows 属性查看器时存在

2024年10月01日8wuhuacong

我需要按拍摄日期提取和整理照片。 Windows 10,Python 2.7。我一直在做这个

from PIL import Image 
def get_date_taken(path): 
    return Image.open(path)._getexif()[36867] 

以下:

Get date and time when photo was taken from EXIF data using PIL

这对一些照片来说效果很好。

太棒了。现在抓取一组不同的图像,新相机,属性看起来相似。

但是字典完全不同

Image.open(image)._getexif()[36867] 
Traceback (most recent call last): 
  Debug Probe, prompt 369, line 1 
KeyError: 36867 
Image.open(image)._getexif() 
{36864: '0220', 37121: '\x01\x02\x03\x00', 40962: 2048, 40963: 1536, 40960: '0100', 40961: 1, 296: 2, 34665: 90, 34855: 1600, 531: 2, 282: (72, 1), 283: (72, 1), 37500: '\x01\xf1\x03\x00\x03\x00\x00\x00\x11 .... 

我也试过exifread

a=exifread.process_file(open(image,'rb')) 
a.keys() 
['EXIF MakerNote', 'Image ExifOffset', 'EXIF ExposureTime', 'EXIF ComponentsConfiguration', 'Image YCbCrPositioning', 'Image XResolution', 'EXIF FlashPixVersion', 'EXIF ISOSpeedRatings', 'Image YResolution', 'EXIF ColorSpace', 'EXIF ExifImageLength', 'EXIF ExifVersion', 'Image ResolutionUnit', 'EXIF ExifImageWidth'] 

但没有拍摄日期。什么是 windows 阅读 python 不是?关于其他尝试的任何建议,我需要担心跨平台吗?与此处相同的问题:

"Date Taken" not showing up in Image PropertyItems while it shows in file details (file properties) in Windows Explorer

但是在 python 中。这个友好的在线元数据查看器

http://exif.regex.info/exif.cgi

表明两个图像在 exif 中都有创建日期标签。还有什么方法可以访问它们?

有问题的示例图像是 here

请您参考如下方法:

我是使用 exifread 库完成的。这是我的 python 代码片段。

import exifread 
for filename in os.listdir(directoryInput): 
    if filename.endswith('.JPG'): 
        with open("%s/%s" % (directoryInput, filename), 'rb') as image: # file path and name 
            exif = exifread.process_file(image) 
            dt = str(exif['EXIF DateTimeOriginal'])  # might be different 
            # segment string dt into date and time 
            day, dtime = dt.split(" ", 1) 
            # segment time into hour, minute, second 
            hour, minute, second = dtime.split(":", 2)