如何将 "Date" 或 "Date taken" 附加到 JPEG 图像文件的名称?

How to append "Date" or "Date taken" to a JPEG image file's name?

这是我的 .jpg 文件的示例:

我想附加“日期”或“拍摄日期”信息 — 而不是“创建日期”或“修改日期”信息。我看到 this answer 但我认为它只会做后者。另外,我希望日期格式为 YYYYMMDD 或 YYYY-MM-DD,不包括时间。有人可以帮忙吗?我知道 python 到 运行 一个脚本,但绝对不足以实际编写或排除故障。 :\

如果有人知道如何在 R 中执行此操作,我会更自在。

如果我正确理解了您的请求,请在下方找到使用包 exifrstringr 解决您的问题的建议方案。针对您的具体情况进行一些调整(即文件扩展名和路径目录),它应该可以工作。

library(exifr)
library(stringr)

# Retrieve all images with extension .jpg from your working directory 
# (the file extension and possibly the file path must be adapted to your case)
files <- list.files(path = getwd(), pattern = "*.jpg")

# Read images Exif metadata
dat <- read_exif(files)

# Retrieve file names and info about dates from Exif metadata
dat[ ,c(grep(pattern = "^FileName", names(dat)), grep(pattern = "Date", names(dat)))]

# Choose the desired date (here, I chose the "FileModifyDate" column which corresponds to the dates the images were taken - i.e. column #2)
chosenDate <- dat[ ,c(grep(pattern = "^FileName", names(dat)), grep(pattern = "Date", names(dat)))][,2]


# Append file names with dates 
# (Do not forget to change the file extension in paste0() according to your case - here the extension is ".jpg") 
file.rename(files, 
            paste0(file_path_sans_ext(files),"_", 
                   gsub(":","-",sapply(chosenDate, stringr::str_extract, ".*(?= )")),
                   ".jpg"))