我在 Python 2.x 中编译了以下脚本,该脚本递归搜索目录,并为找到的每个 JPEG 解析元数据并将其放入字典中。目前,输出只是打印到控制台:-
import os
import fnmatch
import pyexiv2
matches = []
dict1 = {}
# The aim of this script is to recursively search across a directory for all
# JPEG files. Each time a JPEG image is detected, the script used the PYEXIV2
# module to extract all EXIF, IPTC and XMP data from the image. Once extracted
# the key (ie. "camera make" is generated and it's respective value
# (ie. Canon) is then added as the value in a dictionary.
for root, dirnames, filenames in os.walk('C:\Users\XXX\Desktop'):
for filename in fnmatch.filter(filenames, '*.jpg'):
matches.append(os.path.join(root, filename))
for entry in matches:
metadata = pyexiv2.ImageMetadata(entry)
metadata.read()
keys = metadata.exif_keys + metadata.iptc_keys + metadata.xmp_keys
for key in keys:
dict1[key] = metadata[key].raw_value
print entry
print str(dict1)
我要做的是将结果输出到 MySQL DB。现在,我的问题是我没有元数据标题的不确定列表,实际上我一直在努力找到一个,因此,对于我的表格行标题,我希望比较字典键中的每个值(即日期采取,制造,模型等...),如果它不存在于表中,将其作为标题添加到我的表中,然后将 EXIF 数据(键值)输入到相应的列中. 我以前通过 Python 玩过 MySQL,但从未比较行标题并从变量动态创建新的。
谁能指出我正确的方向?