使用Python从目录中的所有图像获取exif数据

时间:2015-04-26 15:46:39

标签: python-2.7

我是Python的新手,我正在尝试编写一个脚本,该脚本将从目录中的所有图像中提取exif数据(然后使用它创建的字典进一步“填充”)。我正在使用Python 2.7,因为它需要与ArcPy一起使用。

我正在努力避免使用pip安装东西,因为它将被不能做这些事情的用户(网络安全)使用,我发现exifread只用一个文件做我想要的,我只需要为了它做更多。

到目前为止,我的脚本如下:

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])

3 个答案:

答案 0 :(得分:1)

我有点慢,但我找到了自己问题的答案。在这里发布以帮助任何其他新手。基本上我只需要将exifread代码作为我的if语句的一部分:

# 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])

答案 1 :(得分:0)

如果你写这篇文章会更容易:

if files.endswith (('jpg','JPG','png','PNG','tiff','TIFF')):
   file_path = os.path.join(dircont, files)
   print file_path
   #Exifread magic
   ...

答案 2 :(得分:0)

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]))