您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“python如何獲取照片拍攝時間”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“python如何獲取照片拍攝時間”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
首先需要安裝exifread庫。通過EXIF(Exchangeable image file format: 可交換圖像文件格式) 獲取這些信息。
獲取圖片時間信息:
import exifread with open(file_path, 'rb') as file_data: tags = exifread.process_file(file_data) tag_date = 'EXIF DateTimeOriginal' if tag_date in tags: file_rename =str(tags[tag_date]).replace(':','').replace(' ', '_') + os.path.splitext(filename)[1] new_path = os.path.join(root_dir, file_rename) os.rename(file_path, new_path)
通過以上代碼即可獲取拍攝時間,得到時間格式:2022:03:11 11:30:06
我們將文件重命名,方便后續(xù)管理。
獲取視頻拍攝時間信息:
format = '%Y%m%d_%H%M%S' file_path = os.path.join(root_dir, filename) statinfo = os.stat(file_path) temp_time = time.localtime(statinfo.st_mtime) file_rename = str(time.strftime(format, temp_time)) + os.path.splitext(filename)[1] new_path = os.path.join(root_dir, file_rename) os.rename(file_path, new_path)
同樣我們將文件 重命名,方便后續(xù)管理。
通過以上操作,照片和視頻文件我們都以時間格式進行命名。接下來我們根據(jù)時間建立文件夾整理。
time_info = os.path.splitext(filename)[0].split("_")[0] dst_dir = save_dir + time_info if not os.path.exists(dst_dir): os.mkdir(dst_dir) src_path = os.path.join(root_dir, filename) save_path = os.path.join(dst_dir, filename) shutil.move(src_path, save_path)
import os import re import time import shutil import exifread def rename_pic(root_dir, filename): file_path = os.path.join(root_dir, filename) try : with open(file_path, 'rb') as file_data: tags = exifread.process_file(file_data) tag_date = 'EXIF DateTimeOriginal' if tag_date in tags: file_rename = str(tags[tag_date]).replace(':', '').replace(' ', '_') + os.path.splitext(filename)[1] new_path = os.path.join(root_dir, file_rename) print(file_path,new_path) os.rename(file_path, new_path) else: print('No {} found'.format(tag_date), ' in: ', file_path) except Exception as e: print("error ", e) def rename_video(root_dir, filename): format = '%Y%m%d_%H%M%S' file_path = os.path.join(root_dir, filename) statinfo = os.stat(file_path) temp_time = time.localtime(statinfo.st_mtime) file_rename = str(time.strftime(format, temp_time)) + os.path.splitext(filename)[1] new_path = os.path.join(root_dir, file_rename) os.rename(file_path, new_path) def rename(root_dir): img_reg = r'(\.JPG|\.PNG|\.jpg|\.png)' video_reg = r'(\.mp4|\.MP4|\.MOV)' for filename in os.listdir(root_dir): file_path = os.path.join(root_dir, filename) if os.path.isfile(file_path): if re.search(img_reg, filename): rename_pic(root_dir, filename) elif re.search(video_reg, filename): rename_video(root_dir, filename) def save_files(root_dir, save_dir): for filename in os.listdir(root_dir): try: time_info = os.path.splitext(filename)[0].split("_")[0] dst_dir = save_dir + time_info if not os.path.exists(dst_dir): os.mkdir(dst_dir) src_path = os.path.join(root_dir, filename) save_path = os.path.join(dst_dir, filename) print(src_path, save_path) shutil.move(src_path, save_path) except Exception as e: print("error ", e) if __name__ == '__main__': root_dir = "/Users/xxx/pics" save_dir = "/Users/xxx/Downloads/" rename(root_dir) save_files(root_dir, save_dir)
讀到這里,這篇“python如何獲取照片拍攝時間”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。