本文小编为大家详细介绍“python如何获取照片拍摄时间”,内容详细,步骤清晰,细节处理妥当,希望这篇“python如何获取照片拍摄时间”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
首先需要安装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
我们将文件重命名,方便后续管理。
获取视频拍摄时间信息:
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)
同样我们将文件 重命名,方便后续管理。
通过以上操作,照片和视频文件我们都以时间格式进行命名。接下来我们根据时间建立文件夹整理。
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如何获取照片拍摄时间”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。