如何将 .csv 文件中列出的图像名称加载到 R 中

发布于 2025-01-11 15:14:40 字数 660 浏览 0 评论 0原文

我使用下面的简单代码将多个图像与 R magick 包一起附加。它运行良好,但是有许多图像需要处理,并且它们的名称存储在 .csv 文件中。任何人都可以建议如何将图像名称从 .csv 文件中的特定单元格加载到 image_read 函数(请参阅代码下面的示例)?到目前为止,我无法找到任何合适的方法来解决这个问题。

library (magick)

pic_A <- image_read('A.png')
pic_B <- image_read('B.png')
pic_C <- image_read('C.png')

combined <- c(pic_A, pic_B, pic_C)
combined <- image_scale(combined, "300x300")
image_info(combined)
final <- image_append(image_scale(combined, "x120"))
print(final)
image_write(final, "final.png") #to save

输入图片此处描述

I am using a simple code below to append multiple images together with the R magick package. It works well, however, there are many images to process and their names are stored in a .csv file. Could anyone advise on how to load the image names to the image_read function from specific cells in a .csv file (see example below the code)? So far, I was not able to find anything appropriate that would solve this.

library (magick)

pic_A <- image_read('A.png')
pic_B <- image_read('B.png')
pic_C <- image_read('C.png')

combined <- c(pic_A, pic_B, pic_C)
combined <- image_scale(combined, "300x300")
image_info(combined)
final <- image_append(image_scale(combined, "x120"))
print(final)
image_write(final, "final.png") #to save

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2025-01-18 15:14:40

像这样的东西应该有效。如果您将 csv 加载到数据框中,则可以直接将 image_read 指向适当的元素。

并且索引(行号)包含在输出文件名中,这样每次迭代就不会被覆盖。

library (magick)

file_list <- read.csv("your.csv",header = F)
names(file_list) <- c("A","B","C")

for (i in 1:nrow(file_list)){
pic_A <- image_read(file_list$A[i])
pic_B <- image_read(file_list$B[i])
pic_C <- image_read(file_list$C[i])

combined <- c(pic_A, pic_B, pic_C)
combined <- image_scale(combined, "300x300")
image_info(combined)
final <- image_append(image_scale(combined, "x120"))
print(final)
image_write(final, paste0("final_",i,".png")) #to save
}

Something like this should work. If you load the csv into a dataframe then, it's then straightforward to point the image_read towards the appropriate elements.

And the index (row number) is included in the output filename so that things are not overwritten each iteration.

library (magick)

file_list <- read.csv("your.csv",header = F)
names(file_list) <- c("A","B","C")

for (i in 1:nrow(file_list)){
pic_A <- image_read(file_list$A[i])
pic_B <- image_read(file_list$B[i])
pic_C <- image_read(file_list$C[i])

combined <- c(pic_A, pic_B, pic_C)
combined <- image_scale(combined, "300x300")
image_info(combined)
final <- image_append(image_scale(combined, "x120"))
print(final)
image_write(final, paste0("final_",i,".png")) #to save
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文