I am looking to make the below code a bit more efficient / OOP based. As can be seen by the X, Y & Z variables, there is a bit of duplication here. Any suggestions on how to make this code more pythonic?
The code functions as I wish and the end result is to import the dictionary values into a MySQL database with the keys being the column headers.
import fnmatch
import os
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\Amy\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()
x = metadata.exif_keys
y = metadata.iptc_keys
z = metadata.xmp_keys
for key in x:
value = metadata[key]
dict1[key] = value.raw_value
for key in y:
value = metadata[key]
dict1[key] = value.raw_value
for key in z:
value = metadata[key]
dict1[key] = value.raw_value
print dict1