python解析照片拍攝時(shí)間進(jìn)行圖片整理
引言
手機(jī)中拍攝照的照片和視頻快爆了,想轉(zhuǎn)移到PC端,并按時(shí)間建立文件夾存儲(chǔ)到電腦中,本文主要介紹如何通過python獲取手機(jī)拍攝圖片的時(shí)間信息并存儲(chǔ)。
1. 獲取圖片拍攝時(shí)間
首先需要安裝exifread庫。通過EXIF(Exchangeable image file format: 可交換圖像文件格式) 獲取這些信息。
獲取圖片時(shí)間信息:
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)通過以上代碼即可獲取拍攝時(shí)間,得到時(shí)間格式:2022:03:11 11:30:06
我們將文件重命名,方便后續(xù)管理。
2. 獲取視頻拍攝時(shí)間
獲取視頻拍攝時(shí)間信息:
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ù)管理。
3. 根據(jù)圖片時(shí)間建立文件夾
通過以上操作,照片和視頻文件我們都以時(shí)間格式進(jìn)行命名。接下來我們根據(jù)時(shí)間建立文件夾整理。
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解析照片拍攝時(shí)間進(jìn)行圖片整理的詳細(xì)內(nèi)容,更多關(guān)于python解析拍攝時(shí)間的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python導(dǎo)入坐標(biāo)點(diǎn)的具體操作
在本篇文章里小編給大家分享了關(guān)于python導(dǎo)入坐標(biāo)點(diǎn)的具體操作步驟和圖解,有需要的朋友們跟著學(xué)習(xí)下。2019-05-05
Python實(shí)現(xiàn)在圖像中隱藏二維碼的方法詳解
隱寫是一種類似于加密卻又不同于加密的技術(shù)。這篇文章主要介紹了如何利用Python語言實(shí)現(xiàn)在圖像中隱藏二維碼功能,感興趣的可以了解一下2022-09-09
python3實(shí)現(xiàn)常見的排序算法(示例代碼)
排序是非常常見的排序算法,今天給大家分享幾種比較常見的排序算法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
Django項(xiàng)目創(chuàng)建到啟動(dòng)詳解(最全最詳細(xì))
這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目創(chuàng)建到啟動(dòng)的步驟,本文介紹的方法算是最全最詳細(xì)的一個(gè)項(xiàng)目,需要的朋友可以參考下2019-09-09

