You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将JPEG图片拍摄日期追加到文件名,求R或Python实现方案

重要提醒:操作前请务必备份所有图片文件,避免重命名错误导致文件丢失或混淆。

R语言实现方案(推荐,你对R更熟练)

首次运行需要先安装读取EXIF信息的依赖包:

install.packages("exifr")

后续直接运行以下脚本即可,可根据注释调整参数:

library(exifr)

# 1. 基础配置,按需修改
# 填写图片所在文件夹路径,Windows系统路径记得把\换成/或者用\\
img_dir <- "C:/你的/图片/文件夹/路径"
# 日期格式二选一:"%Y%m%d"输出20240520,"%Y-%m-%d"输出2024-05-20
date_format <- "%Y%m%d"

# 2. 筛选所有jpg/jpeg格式文件,可自行添加其他图片格式后缀
img_files <- list.files(img_dir, pattern = "\\.(jpg|jpeg)$", full.names = TRUE, ignore.case = TRUE)

# 3. 批量重命名
for (file in img_files) {
  # 仅读取EXIF中的拍摄日期字段,不会读取创建/修改日期
  exif_info <- read_exif(file, tags = "DateTimeOriginal")
  # 跳过无拍摄日期的文件
  if (is.na(exif_info$DateTimeOriginal)) {
    message(paste("文件", basename(file), "无拍摄日期,已跳过"))
    next
  }
  # 格式化日期
  take_date <- format(as.POSIXct(exif_info$DateTimeOriginal), date_format)
  # 生成新文件名:原文件名_日期.后缀
  file_name <- basename(file)
  name_part <- tools::file_path_sans_ext(file_name)
  ext_part <- tools::file_ext(file_name)
  new_file_name <- paste0(name_part, "_", take_date, ".", ext_part)
  new_file_path <- file.path(img_dir, new_file_name)
  # 避免覆盖已存在的文件
  if (!file.exists(new_file_path)) {
    file.rename(file, new_file_path)
    message(paste("已重命名:", file_name, "→", new_file_name))
  } else {
    message(paste("文件", new_file_name, "已存在,已跳过"))
  }
}
Python实现方案

首次运行需要先安装读取EXIF信息的依赖包:

pip install pillow

后续直接运行以下脚本即可,可根据注释调整参数:

import os
from PIL import Image
from PIL.ExifTags import TAGS
from datetime import datetime

# 1. 基础配置,按需修改
# 填写图片所在文件夹路径,Windows系统路径记得把\换成/或者用\\
img_dir = "C:/你的/图片/文件夹/路径"
# 日期格式二选一:"%Y%m%d"输出20240520,"%Y-%m-%d"输出2024-05-20
date_format = "%Y%m%d"

# 2. 遍历文件夹内所有jpg/jpeg文件
for filename in os.listdir(img_dir):
    if not filename.lower().endswith(('.jpg', '.jpeg')):
        continue
    file_path = os.path.join(img_dir, filename)
    # 读取文件EXIF信息
    try:
        img = Image.open(file_path)
        exif_data = img.getexif()
    except:
        print(f"文件{filename}无法读取,已跳过")
        continue
    # 提取拍摄日期字段
    take_date = None
    for tag_id, value in exif_data.items():
        tag = TAGS.get(tag_id, tag_id)
        if tag == "DateTimeOriginal":
            take_date = value
            break
    # 跳过无拍摄日期的文件
    if not take_date:
        print(f"文件{filename}无拍摄日期信息,已跳过")
        continue
    # 格式化日期
    try:
        formatted_date = datetime.strptime(take_date, "%Y:%m:%d %H:%M:%S").strftime(date_format)
    except:
        print(f"文件{filename}日期解析失败,已跳过")
        continue
    # 生成新文件名
    name_part, ext_part = os.path.splitext(filename)
    new_filename = f"{name_part}_{formatted_date}{ext_part}"
    new_file_path = os.path.join(img_dir, new_filename)
    # 避免覆盖已存在的文件
    if not os.path.exists(new_file_path):
        os.rename(file_path, new_file_path)
        print(f"已重命名:{filename} → {new_filename}")
    else:
        print(f"文件{new_filename}已存在,已跳过")

内容的提问来源于stack exchange,提问作者Jon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:36:03