当前位置:首页 > 站长资讯 > 正文内容

【说站】python解析照片拍摄时间进行图片整理

yc8882年前 (2022-07-23)站长资讯238

手机中拍摄照的照片和视频快爆了,想转移到PC端,并按时间建立文件夹存储到电脑中,本文主要介绍如何通过python获取手机拍摄图片的时间信息并存储。

1. 获取图片拍摄时间

首先需要安装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

我们将文件重命名,方便后续管理。

2. 获取视频拍摄时间

获取视频拍摄时间信息:

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)

同样我们将文件 重命名,方便后续管理。

3. 根据图片时间建立文件夹

通过以上操作,照片和视频文件我们都以时间格式进行命名。接下来我们根据时间建立文件夹整理。

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)


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/zhanzhang/7412.html

标签: python
分享给朋友:

“【说站】python解析照片拍摄时间进行图片整理” 的相关文章

【说站】Win10专业版如何激活?Win10激活工具

【说站】Win10专业版如何激活?Win10激活工具

本人用的Win10专业版一直用的好好地,今天桌面右下方就提示“激活Windows 转到设置以激活Windows”,因为一直用的是盗版Windows 10,出现这样的提示也蛮正常,没得办法,见招拆招,在...

【说站】excel筛选两列数据中的重复数据并排序

【说站】excel筛选两列数据中的重复数据并排序

如果靠人眼来一个个的对比excel的两列数据来去重的话,数据量少还能勉强对比一下,如果几千、几万条数据肯定就需要进行程式化处理,excel对于这个问题给我们提供了很方便的解决方案,这里主要用到exce...

【说站】Excel如何快速删除空行?WPS删除excel空白行

【说站】Excel如何快速删除空行?WPS删除excel空白行

站长我经常会处理excel文档,之前介绍过Microsoft Office excel文档删除空行的办法,今天介绍WPS Office下面的excel如何删除空白行。方法一:筛选  选中数据所在的那一...

【说站】判断服务器IP否被墙 是否被TCP阻断

【说站】判断服务器IP否被墙 是否被TCP阻断

现在国内很多购买国外主机服务器的,但往往很多主机商的机子用的人多了,国内使用者用这些服务器做啥的都有,正儿八经的做外贸其实没多大事情,但往往有些人就是不遵守法律法规,长此以往用的人多了,这些国外的主机...

【说站】WordPress网站文章ID不连续如何解决?

【说站】WordPress网站文章ID不连续如何解决?

对于WordPress网站文章ID不连续的问题困扰了我很久,今天将WordPress文章ID不连续的原因和具体解决办法做详细的说明。WordPress文章ID不连续的原因:用WordPress做网站的...

【说站】删除WordPress页脚的 由WordPress强力驱动

【说站】删除WordPress页脚的 由WordPress强力驱动

所周知目前最新版的WordPress搭建的个人博客的2020主题(Twenty_Twenty)在网页的末端会有“由WordPress强力驱动”链接跳转字样,即WordPress的商业推广。作为网站搭建...