小敘:最近參與導師的地籍測量項目,無人機飛回來的照片和pos數據有點問題,博主做內業時接到把照片的經緯度、相機傾角等詳細數據提取出來的任務,奈何到處找資料,頭暈眼花後也沒有得到滿意的解決辦法,最終終於在github上找到了一個炫酷的解決辦法,地址與可獲取信息如下:
https://github.com/CarletonArchives/ExifTool-Batch-Processor
Table. 1.可獲取的圖像exif信息
博主進行環境配置和調試時存在一些問題,在本文中將對調試中的問題進行梳理和解釋,同時將代碼中的Python2部分改寫爲Python3部分,並提供了cmd命令的解決方案。
一、安裝及配置Perl環境
由於ExifTool是使用Perl語言編寫的,在Mac及Linux系統下運行十分方便,但是在windows平臺上首先要進行環境配置,這裏推薦直接從ActiveState官網下載ActivePerl,選擇最新版本下載即可,安裝包下載地址如下:
下載完成後,雙擊進行安裝,注意電腦path環境變量中的自動添加,如果沒有自動添加,則需要手動添加path。windows系統下的Activeperl安裝詳細過程請參考如下鏈接,這裏不做贅述:
https://jingyan.baidu.com/article/9113f81b68e2ce2b3214c7f9.html
二、下載exiftool,並進行簡單配置
1.進入ExifTool官網,下載工具包。
如果採用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. ExifTool工具包選擇
2.pl文件配置
在本文中我們僅對Fig. 1(1)中的文件配置進行特殊說明,Fig. 1(2)的文件配置可參看博主“python學習筆記:應用篇:壓縮包批量解壓的實現(WinRAR、python、cmd)”一文中WinRAR.exe的環境配置與調用方法,博文地址如下:
接下來我們開始進行ExifTool的簡單修改,首先解壓”Image-ExifTool-11.34.tar.gz“文件,在解壓後的文件夾中將“exiftool"文件改名爲"exiftool.pl",記好"exiftool.pl"文件路徑,在博主電腦上路徑爲“D:\Image-ExifTool-11.34\exiftool.pl”。
Fig. 2.對ExifTool文件夾中文件的簡單修改結果
三 、cmd解決方案
1.控制檯指令
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運行結果
2.bat文件
使用bat文件可以解決輸入路徑的麻煩,在txt中輸入如下代碼,另存爲“.bat”後綴文件,放到照片文件夾下,雙擊運行即可
perl D:\Image-Exiftool-11.34\exiftool.pl -csv -r *.jpg > exif.csv
四、Python解決方案
感謝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("<python RunExifTool.py> with no arguments will prompt you for the directory you would like to analyze")
print("<python RunExifTool.py <path to files to validate>> 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()