求一个python脚本,根据文件名字中的时间修改照片和视频的拍摄时间,创建时间,修改时间。文件名不是固定格式,文件可能有重复的,但是文件名中一定有时间。
import re
import time
from win32file import (
CreateFile, SetFileTime, CloseHandle,
GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING,
)
from pywintypes import Time
def parse_time(name: str):
year, month, day = '1990', '01', '01'
year_p = re.search(r"2\d{3}", name)
if year_p is None:
return year, month, day
year = year_p.group()
_, y_e = year_p.span()
name = name[y_e:]
month_p = re.search(r"[01][0-9]?", name)
if month_p is None:
return year, month, day
month = month_p.group()
_, m_e = month_p.span()
name = name[m_e:]
day_p = re.search(r"[0123][0-9]?", name)
if day_p is None:
return year, month, day
day = day_p.group()
return year, month, day
def modify_file_time(file_path):
year, month, day = parse_time(file_path)
modify2time = time.strptime(
year + '-' + month + '-' + day,
"%Y-%m-%d",
)
try:
fh = CreateFile(
file_path, GENERIC_READ | GENERIC_WRITE,
0, None, OPEN_EXISTING, 0, 0,
)
modify_time = Time(modify2time)
SetFileTime(fh, modify_time, modify_time, modify_time)
CloseHandle(fh)
return True
except Exception as e:
print(e)
return False
if __name__ == "__main__":
file_name = r"test2022-05-14.txt" #文件路径
r = modify_file_time(file_name)
print(r)
需要额外安装 pywin32 库,安装指令 pip install pywin32
import re
import time
import os
import pywintypes, win32file, win32con
import piexif
def changeFileTime(fname, newtime):
wintime = pywintypes.Time(newtime)
winfile = win32file.CreateFile(
fname, win32con.GENERIC_WRITE,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None, win32con.OPEN_EXISTING,
win32con.FILE_ATTRIBUTE_NORMAL, None)
win32file.SetFileTime(winfile, wintime, wintime, wintime)
winfile.close()
def changeImageExifShootingTime(fname, newtime):
exif_dict = piexif.load(fname)
timeStr = time.strftime("%Y:%m:%d %H:%M:%S", newtime)
exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = timeStr
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = timeStr
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, fname)
def getTime(fileName):
reg = re.compile(r'\d{4}[-_ ]?\d{2}[-_ ]?\d{2}[-_ ]\d{6}')
m = reg.search(fileName)
item = re.sub('[-_ ]', '', m.group(0))
return time.strptime(item, "%Y%m%d%H%M%S")
filePath="D:\\tmp"
for fileName in os.listdir(filePath):
fileFullPath = os.path.join(filePath, fileName)
newtime = getTime(fileName)
# 修改照片拍摄时间
if fileName.endswith(('.jpg','.jpeg')):
changeImageExifShootingTime(fileFullPath, newtime)
changeFileTime(fileFullPath, newtime)
用了三个文件名字做测试
这不有现成的吗?https://blog.csdn.net/dengnihuilaiwpl/article/details/86551720
如果你的文件名左边开始第一个数字起始的字符串一定是时间的话,也是可以提取到时间信息的,有了时间就可以修改该文件信息达到你的目的了
可以参考一下呢 http://www.ay1.cc/article/20846.html
# 导入pyexiv2包
from pyexiv2 import Image
# 读取图片
img = Image('./weihuang.jpg')
# 打印EXIF、IPTC、XMP信息
print(img.read_exif())
print(img.read_iptc())
print(img.read_xmp())
# 用字典记录目标时间信息
exif_dict = {'Exif.Image.DateTime': '2022:05:25 18:57:55',
'Exif.Photo.DateTimeOriginal': '2022:05:25 19:15:47',
'Exif.Photo.DateTimeDigitized': '2022:05:25 19:15:47'}
iptc_dict = {'Iptc.Application2.DateCreated': '2022-05-25'}
xmp_dict = {'Xmp.xmp.ModifyDate': '2022-05-25T18:57:55+08:00',
'Xmp.xmp.CreateDate': '2022-05-25T19:15:47',
'Xmp.xmp.MetadataDate': '2022-05-25T18:57:55+08:00',
'Xmp.photoshop.DateCreated': '2022-05-25T19:15:47.004',
'Xmp.xmpMM.History[1]/stEvt:when': '2022-05-25T18:57:55+08:00',
'Xmp.xmpMM.History[2]/stEvt:when': '2022-05-25T18:57:55+08:00'}
# 修改EXIF、IPTC、XMP信息
img.modify_exif(exif_dict)
img.modify_iptc(iptc_dict)
img.modify_xmp(xmp_dict)
用python脚本分类和改名照片和视频
https://blog.csdn.net/lijun070118/article/details/127460041