我有一个图像目录,我想动态地组合3-10个图像。它将在任何地方从3-10张图片.我的想法是创建n个变量,然后将这些n个变量传递给image_append
。有没有办法把我的image1,image2,image3的名单.去image_append
?
library(magick)
these=list.files('../Desktop/',pattern = '.tif') ##list of images, could be 3-10
for (h in 1:3){
assign(paste("image", h, sep = ""), image_read(these[h]) %>%
image_annotate(.,strsplit(these[h],'_')[[1]][4],color = 'white',size=30))
}
image_append(c(image1,image2,image3)) ##Works, but there will be an unknown number of *image* vars created
combine_images = function(...){z=image_append(c(...));return(z)} ##Function that can combine a dynamic number, but passing ls(pattern='image') does not work
发布于 2020-04-13 21:50:33
与其将图像存储在全局环境中,不如将其存储在列表中。这样,您就可以不用循环,只需lapply
您的呼叫:
library(magick)
these <- list.files('../Pictures/', pattern = '.tif', full.names = TRUE)
pictures <- image_append(do.call("c", lapply(these, function(h){
image_annotate(image_read(h), strsplit(h, '[.]')[[1]][1], color = 'white', size = 30)
})))
现在,在我的例子中,我得到了以下结果:
pictures
https://stackoverflow.com/questions/61196196
复制相似问题