我有一个很好的句子列表,我试图将每个注释添加到同一模板图像上并分别保存每个图像文件(即,我将拥有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"))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句