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

本文介绍了一种情况,即从百度网盘下载的JPEG照片创建时间显示为下载时间,而非EXIF中的实际时间。由于Apple照片程序遵循隐私策略,作者通过编写Python脚本读取JPEG的EXIF信息,修正文件的创建时间和文件名,以匹配原始拍摄时间。

之前把照片保存在百度网盘上了,重新下载之后照片的创建时间变成了下载时间。但是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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值