我有一個包含數千張圖片的檔案夾,每張圖片都需要添加一個唯一的關鍵字串列。我還有一個表格,其中包含顯示檔案路徑和每個影像所需關鍵字的關聯串列的欄位。例如,一條記錄可能需要標簽“ORASH(調查地點代碼),Crew 1,Transect A Upstream,Site Layout”。雖然下一條記錄可能需要標簽,“ORWLW、Crew 2、兩棲動物、太平洋大鯢”。
我如何遍歷每個影像以向它們添加 IPTC 關鍵字?我正在使用 python 3 和 iptcinfo3 模塊,但我愿意嘗試其他可能有效的模塊。
這是我現在所在的位置:
import os
import pandas as pd
from iptcinfo3 import IPTCInfo
srcdir = r'E:\photos'
files = os.listdir(srcdir)
# Create a dataframe from the table containing filepaths and associated keywords.
df = pd.read_excel(r'E:\photo_info.xlsx')
# Create a dictionary with the filename as the key and the tags as the value.
references = dict(df.set_index('basename')['tags'])
for file in files:
# Get the full filepath for each image.
filepath = os.path.join(srcdir, file)
# Create an object for a file that may not have IPTC data (ignore the 'Marker scan...' notification).
info = IPTCInfo(filepath, force=True)
在這一點上,我想象我會info['keywords'] = ...結合使用“參考”字典將關鍵字插入正確的檔案中。然后info.save_as(filepath)。我只是沒有足夠的經驗來知道如何使這項作業有效,即使這是一種合理的方法。任何幫助,將不勝感激!
uj5u.com熱心網友回復:
我將包含檔案名和關鍵字的表格保存為 .csv 檔案,其中的欄位和記錄如下所示(盡管“主題”欄位中的文本不包含引號):
源檔案、藝術家、主題
E:\photos\0048.JPG,MARY GRAY,“YEAR2022,需要,帶時間的 GPS 裝置”
因為我將 Jupyter Lab 用于此作業流的其他部分,所以我在那里運行了這段代碼:
import os
os.system('cmd d: & exiftool -overwrite_original -sep ", " -csv="E:\photos\metadata.csv" E:\photos')
這將打開 Windows 命令提示符,將路徑更改為 D: 驅動器(保存 exiftool.exe 檔案的位置),呼叫 exiftool,將其設定為覆寫原始影像檔案而不是創建副本,在 . csv 檔案,讀取包含檔案名串列和相關關鍵字的 .csv 檔案,然后在 E:\photos 目錄中運行它。
處理了大約 4,000 張照片,效果驚人!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/534356.html
