Reputation: 47
I'm pretty new to Python and I am trying to write a script which will extract the exif data from all the images in a directory (and then do further "stuff" with the dictionary it creates). I'm using Python 2.7 as it needs to work with ArcPy.
I am trying to avoid having to install things using pip as it will be being used by users who can't do such things (network security) and I have found exifread does what I want with just one file, I just need for it to do it with more.
So far the script I have is as follows:
import os
import exifread
# Select the directory which the image files are in
dircont = raw_input("Please select the directory which you wish to retrieve geotags from ")
# Get the list of image files
directory = os.listdir(dircont)
for files in directory:
if files.endswith ('.jpg'):
file_path = os.path.join(dircont, files)
print file_path
if files.endswith ('.JPG'):
file_path = os.path.join(dircont, files)
print file_path
if files.endswith ('.png'):
file_path = os.path.join(dircont, files)
print file_path
if files.endswith ('.PNG'):
file_path = os.path.join(dircont, files)
print file_path
if files.endswith ('.tiff'):
file_path = os.path.join(dircont, files)
print file_path
if files.endswith ('.TIFF'):
file_path = os.path.join(dircont, files)
print file_path
# EXIFREAD CODE
# Open image file for reading (binary mode)
f = open(file_path, 'rb')
# Return Exif tags
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
Upvotes: 1
Views: 3528
Reputation: 1
import exifread
import os,sys
dircont = input("Please Enter the path of your directory ")
directory = os.listdir(dircont)
for files in directory:
if files.endswith (('jpg','JPG','png','PNG','tiff','TIFF')):
file_path = os.path.join(dircont, fil)
print (file_path)
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print ("Key: %s, value %s" % (tag, tags[tag]))
Upvotes: 0
Reputation: 16
It's easier if you write this:
if files.endswith (('jpg','JPG','png','PNG','tiff','TIFF')):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
...
Upvotes: 0
Reputation: 47
I was being a bit slow but I have found the answer to my own question. Posting here to help any other newbies out. Basically I just had to put the exifread code as part of my if statements:
# Select the directory which the image files are in
dircont = raw_input("Please select the file which you wish to retrieve geotags from ")
# Get the list of image files in the directory that exifread supports
directory = os.listdir(dircont)
for files in directory:
if files.endswith ('.jpg'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
if files.endswith ('.JPG'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
if files.endswith ('.png'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
if files.endswith ('.PNG'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
if files.endswith ('.tiff'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
if files.endswith ('.TIFF'):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
f = open(file_path, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
print "Key: %s, value %s" % (tag, tags[tag])
Upvotes: 1