在 R 上使用 Magick 写入多个图像
Writing multiple images using Magick on R
我有一个 excel 句子列表,我试图将每个句子注释到同一个模板图像上并单独保存每个图像文件(即我将有 20 张图像,具有相同的背景但不同的文本- 类似于 Word 的文档邮件合并功能)。
为此,我使用了 R 包 magick
的 image_annotate
和 image_write
命令。我的假设是将其放入 for
循环中即可完成此任务。
我使用了以下代码:
QuoteList=read.csv("wordlist.csv", stringsAsFactors = F, header = T)
myTemplate=image_read("template.png")
for (i in 1:nrow(QuoteList))
{
thisImage[i]=image_annotate(myTemplate, QuoteList$myquote[i])
image_write(thisImage[i], format = "png")
}
但是我收到错误:
Error in magick_image_replace(x, i, value) : subscript out of bounds
我不完全确定我做错了什么,非常感谢对此或任何可能的替代解决方案的任何帮助。提前致谢。
您需要在每次迭代中替换您正在写入的对象,但每次写入一个新文件名:
for (i in 1:nrow(QuoteList)){
thisImage = image_annotate(myTemplate, QuoteList$myquote[i])
image_write(thisImage, format <- "png", path = paste0(i, ".png"))
}
我有一个 excel 句子列表,我试图将每个句子注释到同一个模板图像上并单独保存每个图像文件(即我将有 20 张图像,具有相同的背景但不同的文本- 类似于 Word 的文档邮件合并功能)。
为此,我使用了 R 包 magick
的 image_annotate
和 image_write
命令。我的假设是将其放入 for
循环中即可完成此任务。
我使用了以下代码:
QuoteList=read.csv("wordlist.csv", stringsAsFactors = F, header = T)
myTemplate=image_read("template.png")
for (i in 1:nrow(QuoteList))
{
thisImage[i]=image_annotate(myTemplate, QuoteList$myquote[i])
image_write(thisImage[i], format = "png")
}
但是我收到错误:
Error in magick_image_replace(x, i, value) : subscript out of bounds
我不完全确定我做错了什么,非常感谢对此或任何可能的替代解决方案的任何帮助。提前致谢。
您需要在每次迭代中替换您正在写入的对象,但每次写入一个新文件名:
for (i in 1:nrow(QuoteList)){
thisImage = image_annotate(myTemplate, QuoteList$myquote[i])
image_write(thisImage, format <- "png", path = paste0(i, ".png"))
}