0

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

图像文件列表的屏幕截图

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

如果有人知道如何在 R 中做到这一点,那么我在那里会更舒服。

4

1 回答 1

1

如果我正确理解了您的要求,请在下面找到使用包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"))

于 2021-10-02T15:09:31.993 回答