我需要知道所拍摄图像数据的属性(天,时间,小时,分钟,秒)

import exifread
import os
directoryInput=r"C:\tekstilshiki"
for filename in os.listdir(directoryInput):
    if filename.endswith('.jpg'):
        with open(r"%s\%s" % (directoryInput, "11.jpg"), 'rb') as image: # directory and name bleat
            exif = exifread.process_file(image)
            dt = str(exif['EXIF DateTimeOriginal'])
            # into date and time
            day, dtime = dt.split(" ", 1)
            hour, minute, second = dtime.split(":", 2)

当您运行脚本时出现错误



我认为标签名称不正确

How can I read from all EXIF properties only the key time and the capture dateng

最佳答案

根据从图像中提取的内容,“exif”的每个瞬间都可以包含不同的键,因此,为避免显示“KeyError”消息,您需要检查“exif”是否包含键“EXIF DateTimeOriginal”:

import exifread, os

directoryInput=r"C:\tekstilshiki"
for filename in os.listdir(directoryInput):
    if filename.endswith('.jpg'):
        with open(os.path.join(directoryInput, filename), "rb") as image: # Change "11.jpg" to filename variable
            exif = exifread.process_file(image)
            if "DateTimeOriginal" in exif:
                dt = str(exif["EXIF DateTimeOriginal"])
                # into date and time
                day, dtime = dt.split(" ", 1)
                hour, minute, second = dtime.split(":", 2)

聚苯乙烯

尽管您已经使用'os.listdir'在选定目录中找到了所有文件,但是在第6行中,您已经硬编码了同一文件“11.jpg”。

关于python - 当您运行脚本KeyError : 'EXIF DateTimeOriginal' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45651080/

10-15 01:33