如何一次image_read多个图像?

时间:2018-04-02 13:29:43

标签: r image imagemagick

为了使用magick包创建.gif文件,我怎样才能一次读取多个图像?

我在列表对象中成功导入它们,但从image_animate()收到错误。

# read all files in folder (only .png files)
capturas <- list.files("./path/to/images/")

# get all images in a list
images <- vector()
for (i in seq_along(capturas)) {
  images[i] <- list(image_read(str_c("./path/to/images/", capturas[i])))}

image_animate(image_scale(images, "500x500"), fps = 1, dispose = "previous")

收到以下错误:

> image_animate(image_scale(images, "500x500"), fps = 2, dispose = "previous")
Error: The 'image' argument is not a magick image object.

在每张图片上分别使用image_read时可以正常工作......

img_1 <- image_read(str_c("./path/to/images/", capturas[1]))
img_2 <- image_read(str_c("./path/to/images/", capturas[2]))
img_3 <- image_read(str_c("./path/to/images/", capturas[3]))
img <- c(img_1, img_2, img_3)
img <- image_scale(img, "300x300")

2 个答案:

答案 0 :(得分:0)

您可以使用magick软件包中的vars创建可以在image_join命令中使用的多帧图像。我使用了image_animate软件包而不是循环。

purrr

答案 1 :(得分:0)

这个对我有用

library(magick)

#Get the list of all images: (".jpg", ".png", etc) 
allFiles = list.files(path = "./path/to/images/", pattern = ".jpg", full.names = T)

#Get the dataframe with info
allInfo = image_info(image_read(allFiles))

#Read the images 
my_images = image_read(allFiles)

改编自:https://community.rstudio.com/t/import-multiple-images-in-a-folder-and-image-info/43129/2

如果需要动画:

animation <- image_animate(image_scale(my_images, "200x200"), fps = 1) 
animation

保存动画:

image_write(animation, "./path/to/output/test1.gif")