Reputation: 439
I have a directory of images and I want to combine anywhere from 3-10 of the images dynamically. It will be anywhere from 3-10 images. My thought was to create n variables and just pass those n variables to image_append
. Is there a way to pass my list of image1,image2,image3... to 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
Upvotes: 0
Views: 520
Reputation: 173858
Instead of storing the images in the global environment, store it in a list. That way, instead of looping, you can just lapply
your calls:
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)
})))
So now, in my case, I get the following result:
pictures
Upvotes: 2