iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python解析照片拍摄时间进行图片整理
  • 287
分享到

python解析照片拍摄时间进行图片整理

2024-04-02 19:04:59 287人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录引言1. 获取图片拍摄时间2. 获取视频拍摄时间3. 根据图片时间建立文件夹完整代码引言 手机中拍摄照的照片和视频快爆了,想转移到PC端,并按时间建立文件夹存储到电脑中,本文主要

引言

手机中拍摄照的照片和视频快爆了,想转移到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)

以上就是Python解析照片拍摄时间进行图片整理的详细内容,更多关于python解析拍摄时间的资料请关注编程网其它相关文章!

--结束END--

本文标题: python解析照片拍摄时间进行图片整理

本文链接: https://www.lsjlt.com/news/119568.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作