英文:
How to load/alter image tags from R?
问题
我想通过更改图像的元数据来为图像添加'tags'。我认为我可以使用exiftool来开始这个过程,但我仍然不确定如何操作。供参考,我有一个包含我想要添加到图像的标签的.csv文件,.csv文件看起来像这样:
文件 | 类别 |
---|---|
IMAG0001.JPG | 人物 |
IMAG0002.JPG | 动物 |
现在我想让从'类别'列加载的内容作为元数据加载到图像上,最好是作为'tags'(这样你可以在诸如digikam等程序中看到它们)。我不确定更改元数据的具体代码是什么。
我知道可以使用以下方式更改图像的元数据:
exiftool("image.jpg", "Author=John Doe", "Comment=This is a comment")
但我不知道如何一次批量处理多个图像。
英文:
I want to add 'tags' to images by altering their metadata. I think I could use exiftool to start this process but am still unsure of how to go about this. For reference I have a .csv file with the tags I would like to add to images, the .csv file looks something like this:
file | category |
---|---|
IMAG0001.JPG | Human |
IMAG0002.JPG | Animal |
Now I would like to make it possible to load thing from the 'category' column onto images as metadata, preferably as 'tags' (such that you could see them in programs such as digikam). I'm not sure what the specific code for altering metadata.
I know that is is possible to alter metadata of an image using
exiftool("image.jpg", "Author=John Doe", "Comment=This is a comment")
but I don't know how to use do this in mass with many images at once.
答案1
得分: 1
有两个主要的包可供使用,分别是 exifr
和 exiftoolr
。我将使用 exiftoolr
,因为它使用了 system2()
,与 exifr
使用的 system()
不同,它在 Windows 和基于 Unix 的计算机(如 Mac 和 Linux)上都兼容。
设置
首先,我们安装这些包,并准备好一切:
# 如果其中一个给出错误,请使用 install.packages() 安装它们
library(exiftoolr)
library(tidyverse)
# 安装 exiftool
install_exiftool()
编辑一张图片
我已经从维基百科上下载了这张照片:
假设我们想要更改艺术家的名称(为了捉弄我们的朋友)。
要读取当前的exif数据,我们可以对文件使用 exif_read()
,然后我们会得到一个数据框,我们可以从中选择属性:
exif_read("/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg") %>% select(Artist)
# Artist
# 1 Praveen. P
现在,要进行更改,我们运行 exif_call
,带上我们的参数:
exif_call(args = "-Artist=\"Mark Hill\"", path="/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg")
[1] " 1 image files updated"
现在,当我们再次运行 exif_read()
时,艺术家属性已经改变了:
exif_read("/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg") %>% select(Artist)
# Artist
#1 "Mark Hill"
编辑多张图片
要多次执行这个操作,我将假设所有文件都在当前工作目录中。
加载你的数据框:
df <- data.frame(
file = c("IMAG0001.JPG", "IMAG0002.JPG"),
category = c("Human", "Animal")
)
然后,只需要循环遍历文件,应用你想要做的事情:
for (i in 1:nrow(df)){
exif_call(path = image_file_paths[i], args = paste0("-XMP:Category=\"", df$category[i], "\""))
}
英文:
There are two main packages you can use exifr
and exiftoolr
. I'm going to use exiftoolr
, because it uses system2(), which, unlike system()
(used by exifr
) is compatible with both Windows and Unix-based computers (e.g. Mac and Linux)
Set-up
First, we install the packages, and get everything up and running:
# if either of these give you errors, install them with install.packages()
library(exiftoolr)
library(tidyverse)
# install exiftool
install_exiftool()
Edit one image
I've downloaded this photo from Wikipedia:
Say we want to change the name of the artist (to play a prank on our friends).
To read the current exif data, we can exif_read() on the file, and we get back a dataframe, which we can select attributes from:
exif_read("/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg") %>% select(Artist)
# Artist
# 1 Praveen. P
Now, to change it, we run exif_call
, with our argument(s):
exif_call(args = "-Artist=\"Mark Hill\"", path="/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg")
[1] " 1 image files updated"
Now, when we run exif_read()
again, the Artist attribute has changed:
exif_read("/Users/pc/Downloads/Epepeotes_uncinatus_@_Kanjirappally.jpg") %>% select(Artist)
# Artist
#1 "Mark Hill"
Edit many images
To do it many times, I'm going to assume all of the files are in the current working directory.
Loading your dataframe:
df <- data.frame(
file = c("IMAG0001.JPG", "IMAG0002.JPG"),
category = c("Human", "Animal")
)
Then, it's simply a matter of looping through the files, applying the things you want to do:
for (i in 1:nrow(df)){
exif_call(path = image_file_paths[i], args = paste0("-XMP:Category=\"", df$category[i], "\""))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论