How can I image_read multiple images at once?

[亡魂溺海] 提交于 2019-12-22 08:13:36

问题


In order to create a .gif using the magick package, how could I read multiple images at once?

I'm importing them succesfully in a list object, but getting an error from 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")

Getting the following error:

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

While using image_read on each image separately works OK...

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")

回答1:


You can use image_join from the magick package to create a multi-frame image you can use in the image_animate command. I used the purrr package instead of a loop.

library(purrr)
library(magick)
capturas <- list.files("./path/to/images/", pattern = "\\.png$")

# get all images in a list
images <- map(capturas, image_read)
images <- image_join(images)

image_animate(images, fps = 1, dispose = "previous")


来源:https://stackoverflow.com/questions/49612276/how-can-i-image-read-multiple-images-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!