欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python解析照片拍攝時間進行圖片整理

 更新時間:2022年07月22日 16:42:39   作者:languageX  
這篇文章主要為大家介紹了python解析照片拍攝時間進行圖片整理的示例源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

引言

手機中拍攝照的照片和視頻快爆了,想轉(zhuǎn)移到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

我們將文件重命名,方便后續(xù)管理。

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)

同樣我們將文件 重命名,方便后續(xù)管理。

3. 根據(jù)圖片時間建立文件夾

通過以上操作,照片和視頻文件我們都以時間格式進行命名。接下來我們根據(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解析照片拍攝時間進行圖片整理的詳細內(nèi)容,更多關(guān)于python解析拍攝時間的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 利用PyQT5日期控件制作一個小日歷

    利用PyQT5日期控件制作一個小日歷

    這篇文章主要介紹了利用PyQT5的日期控件制作一個小日歷,因為pyqt5已經(jīng)自帶了相關(guān)的日期控件,只需要明白如何調(diào)用再加上比較個性化的功能,這個日歷的小控件就制作完成了。需要的可以參考一下
    2022-01-01
  • python django使用haystack:全文檢索的框架(實例講解)

    python django使用haystack:全文檢索的框架(實例講解)

    下面小編就為大家?guī)硪黄猵ython django使用haystack:全文檢索的框架(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Python itertools模塊詳解

    Python itertools模塊詳解

    這篇文章主要介紹了Python itertools模塊詳解,本文基本是基于文檔的翻譯和補充,相當(dāng)于翻譯了,需要的朋友可以參考下
    2015-05-05
  • python?使用OpenCV進行曝光融合

    python?使用OpenCV進行曝光融合

    這篇文章主要介紹了python?使用OpenCV進行曝光融合,使用OpenCV的Exposure?Fusion,曝光融合是一種將使用不同曝光設(shè)置拍攝的圖像合成為一張看起來像色調(diào)映射的高動態(tài)范圍(HDR)圖像的圖像的方,下文更多詳細內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-04-04
  • Python中*args與**kwargs的高級應(yīng)用指南

    Python中*args與**kwargs的高級應(yīng)用指南

    在Python編程中,*args和**kwargs是兩個非常強大的功能,它們允許開發(fā)者構(gòu)建更加靈活和可擴展的函數(shù),下面就跟隨小編一起來看看它的具體應(yīng)用吧
    2024-03-03
  • PyTorch之關(guān)于hook機制

    PyTorch之關(guān)于hook機制

    這篇文章主要介紹了PyTorch之關(guān)于hook機制的理解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python計算回文數(shù)的方法

    Python計算回文數(shù)的方法

    這篇文章主要介紹了Python計算回文數(shù)的方法,實例分析了Python操作字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 詳解如何在ChatGPT內(nèi)構(gòu)建一個Python解釋器

    詳解如何在ChatGPT內(nèi)構(gòu)建一個Python解釋器

    這篇文章主要為大家詳細介紹了如何在ChatGPT內(nèi)構(gòu)建一個Python解釋器,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,需要的可以參考一下
    2023-02-02
  • python中精確輸出JSON浮點數(shù)的方法

    python中精確輸出JSON浮點數(shù)的方法

    這篇文章主要介紹了python中精確輸出JSON浮點數(shù)的方法,需要的朋友可以參考下
    2014-04-04
  • Python 讀取位于包中的數(shù)據(jù)文件

    Python 讀取位于包中的數(shù)據(jù)文件

    這篇文章主要介紹了Python 如何讀取位于包中的數(shù)據(jù)文件,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08

最新評論