首页 > 编程语言 > 详细

python获取照片的拍摄日期并重命名

时间:2020-12-10 21:37:32      阅读:43      评论:0      收藏:0      [点我收藏+]
# -*- coding: UTF-8 -*-
# python 3.9.0 64bit

import os
import exifread


def renamePic(dir):
    shootingTimeField = ‘EXIF DateTimeOriginal‘
    for root, dirs, files in os.walk(dir):
        for name in files:
            picName = os.path.join(root, name)
            fd = open(picName, ‘rb‘)
            tags = exifread.process_file(fd)
            fd.close()
            if shootingTimeField in tags:
                info = str(tags[shootingTimeField])
                newName = ‘IMG_‘ + info[0:10] + ‘_‘ + info[11:11 + 8] + os.path.splitext(picName)[1]
                newName = newName.replace(‘:‘, ‘‘)
                newName = os.path.join(root, newName)

                count = 1
                while os.path.exists(newName):
                    newName = ‘IMG_‘ + info[0:10] + ‘_‘ + info[11:11 + 8] + ‘_‘ + str(count) + os.path.splitext(picName)[1]
                    newName = newName.replace(‘:‘, ‘‘)
                    newName = os.path.join(root, newName)
                    count += 1

                os.rename(picName, newName)
                print(‘{} --> {}‘.format(os.path.basename(picName), os.path.basename(newName)))
            else:
                print(‘pass: {}‘.format(os.path.basename(picName)))


if ‘__main__‘ == __name__:
    dir = input(‘Please input picture directory path which you want to rename: ‘)
    print()
    renamePic(dir)
    print()
    print(‘Done!‘)
    input(‘Press any key to exit!‘)

 

python获取照片的拍摄日期并重命名

原文:https://www.cnblogs.com/sinicheveen/p/14116272.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!