小叙:最近参与导师的地籍测量项目,无人机飞回来的照片和pos数据有点问题,博主做内业时接到把照片的经纬度、相机倾角等详细数据提取出来的任务,奈何到处找资料,头晕眼花后也没有得到满意的解决办法,最终终于在github上找到了一个炫酷的解决办法,地址与可获取信息如下:
https://github.com/CarletonArchives/ExifTool-Batch-Processor
Table. 1.可获取的图像exif信息
博主进行环境配置和调试时存在一些问题,在本文中将对调试中的问题进行梳理和解释,同时将代码中的Python2部分改写为Python3部分,并提供了cmd命令的解决方案。
由于ExifTool是使用Perl语言编写的,在Mac及Linux系统下运行十分方便,但是在windows平台上首先要进行环境配置,这里推荐直接从ActiveState官网下载ActivePerl,选择最新版本下载即可,安装包下载地址如下:
https://www.activestate.com/products/activeperl/downloads/
下载完成后,双击进行安装,注意电脑path环境变量中的自动添加,如果没有自动添加,则需要手动添加path。windows系统下的Activeperl安装详细过程请参考如下链接,这里不做赘述:
https://jingyan.baidu.com/article/9113f81b68e2ce2b3214c7f9.html
如果采用Python的解决方案,则下载Fig. 1(1);如果采用cmd解决方案,则下载Fig. 1(1)/Fig. 1(2)皆可,只需要在输入指令时注意程序的调用名称即可。
https://sno.phy.queensu.ca/~phil/exiftool/
ExifTool官网
https://sno.phy.queensu.ca/~phil/exiftool/exiftool_pod.html#reading_examples
ExifTool 帮助文档
在本文中我们仅对Fig. 1(1)中的文件配置进行特殊说明,Fig. 1(2)的文件配置可参看博主“python学习笔记:应用篇:压缩包批量解压的实现(WinRAR、python、cmd)”一文中WinRAR.exe的环境配置与调用方法,博文地址如下:
https://blog.csdn.net/knkn123/article/details/83020382
接下来我们开始进行ExifTool的简单修改,首先解压”Image-ExifTool-11.34.tar.gz“文件,在解压后的文件夹中将“exiftool"文件改名为"exiftool.pl",记好"exiftool.pl"文件路径,在博主电脑上路径为“D:\Image-ExifTool-11.34\exiftool.pl”。
Fig. 2.对ExifTool文件夹中文件的简单修改结果
win+R,输入cmd打开控制台,输入如下代码,注意空格!
perl D:\Image-Exiftool-11.34\exiftool.pl -csv -r E:\19040501\100MEDIA > E:\19040501\100MEDIA\data\meta\exif.csv
perl 【exiftool.pl路径+文件名】 -csv -r 【image文件路径】 > 【CSV文件数据路径+CSV文件名】
回车运行,结果如Fig. 3所示,此时便可以在对应路径下找到输出的CSV文件。
Fig.3 .cmd运行结果
使用bat文件可以解决输入路径的麻烦,在txt中输入如下代码,另存为“.bat”后缀文件,放到照片文件夹下,双击运行即可
perl D:\Image-Exiftool-11.34\exiftool.pl -csv -r *.jpg > exif.csv
感谢Caitlin Donahue分享的原始代码,博主对代码内部进行了一定的修改,使其在Python3环境下可运行,并对关键参数进行了标注,亲测,完美运行。
import os
import sys
import platform
import errno
def usage_message():
print("RunExifTool is a program to automatically run exiftool on a set of bags contained within a directory supplied by the user")
print( "--------------------")
print("Usage:")
print("-h or --help to display this message")
print(" with no arguments will prompt you for the directory you would like to analyze" )
print("> will run exiftool on the supplied directory" )
print("--------------------")
print("Dependencies:")
print("Must have exiftool installed on your computer")
print("Must have perl version 5.004 or newer n your computer")
def main():
#环境参数设置
ExifTool_path = 'D:\\Image-ExifTool-11.34\\exiftool.pl' # 根据exiftool.pl文件的路径进行设置
Image_bag_path = 'E:\\19040501\\' # 根据待处理图像的路径进行设置
#store the original directory so we can navigate back to it at the end of the program
original_location = os.getcwd()
parent = ""
exif = ""
exifName = ""
if len(sys.argv) == 2:
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
usage_message()
else:
parent = sys.argv[1]
if parent == "":
#get file locations from the user
print("Please enter the directory that holds the bags you would like to scan: ")
#parent = input().strip()
parent = Image_bag_path.strip()
#check what os the user is running to account for terminal command differences
if platform.system() == "Windows":
exifName = "exiftool.pl"
else:
exifName = "exiftool"
#make sure the directories are in the correct format
parent = parent.strip().strip("'").strip('"')
#navigate to the file that the user's exif program is located in
#获取当前路径下的文件夹列表
path_list = [x for x in os.listdir(parent)]
for folder in path_list:
full_folder_path = os.path.join(parent, folder).strip()
if os.path.isdir(full_folder_path):
print("--------------------------------------")
print("Running exif tool on files in " + full_folder_path)
print("--------------------------------------")
meta_path = os.path.join(full_folder_path,"data","meta")
print("--------------------------------------")
print( "Making sure " + meta_path + " exists.")
print( "--------------------------------------")
try:
os.makedirs(meta_path)
#If an error is raised
#ignore it if it is telling us the file already exists
#raise the error if it is related to something else
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
full_folder_path = ('"' + full_folder_path + '"')
meta_path = ('"' + meta_path + '"')
print( "--------------------------------------")
print( "Making xml file in " + meta_path)
print("--------------------------------------")
cmd = "perl {0} -csv -r {1} > {2}".format(ExifTool_path, full_folder_path.strip('"'), os.path.join(meta_path.strip('"'), "exif.xml").strip('"'))
os.system(cmd)
print("--------------------------------------")
print( "Making csv file in " + meta_path)
print( "--------------------------------------")
cmd = "perl {0} -csv -r {1} > {2}".format(ExifTool_path, full_folder_path.strip('"'), os.path.join(meta_path.strip('"'), "exif.csv").strip('"'))
os.system(cmd)
print( "--------------------------------------")
print("Run on " + full_folder_path + " complete")
print("--------------------------------------")
print("--------------------------------------")
print("Run Complete")
print("--------------------------------------")
main()