Mac下JPEG照片的时间日期纠正

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Q52077987/article/details/80881105

之前把照片保存在百度网盘上了,重新下载之后照片的创建时间变成了下载时间。但是EXIF中的创建时间是正确的。可是Apple的照片程序竟然显示的是照片的文件创建时间而不是EXIF中的时间,开始以为这是一个Bug,后来明白了,应该是Apple的用户的隐私策略限制,不能读取用户的文件(至少看起来是这样的)。而百度网盘似乎就不太在乎这些,在百度网盘上的文件是会自动按EXIF文件中的时间整理的。

为此,写了一个python脚本,读取JPEG的EXIF中的原始创建时间,改写文件的创建时间和文件名。

from PIL import Image
import exifread
import os


def renameFile(fileName, newName):
    if fileName == newName:
        return
    tot = 1
    while os.path.exists(newName):
        newName = os.path.split(fileName)[0] + "_" + str(tot) + os.path.split(fileName)[1]
        tot += 1
    os.rename(fileName, newName)


def editFileCtime(filePath, timeStr):
    print("====editFileCtime")
    ctime = timeStr.replace(':', '').replace(' ', '')
    ctime = ctime[:-2] + "." + ctime[-2:]
    os.system("touch -t " + ctime + " " + filePath)


def getCreateTime(filename):
    print("====getCreateTime")
    FIELD = 'EXIF DateTimeOriginal'
    fd = open(filename, 'rb')
    tags = exifread.process_file(fd)
    fd.close()

    print("fileName:", filename)

    if FIELD in tags:
        print(tags[FIELD])
        return tags[FIELD]
    else:
        print('No {} found'.format(FIELD))
        return None


if __name__ == "__main__":
    for filename in os.listdir('.'):
        if os.path.isfile(filename):
            createTime = getCreateTime(filename)
            if createTime:
                editFileCtime(filename, str(createTime))
                new_name = str(createTime).replace(':', '').replace(' ', '_') + os.path.splitext(filename)[1]
                renameFile(filename, new_name)

猜你喜欢

转载自blog.csdn.net/Q52077987/article/details/80881105
今日推荐