如何一次读取多个图像? [英] How can I image_read multiple images at once?

查看:78
本文介绍了如何一次读取多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用magick包创建.gif,我如何一次读取多张图像?

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

我已将它们成功导入列表对象中,但是从image_animate()中收到错误.

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

出现以下错误:

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

在每个图像上分别使用image_read时,效果很好...

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

推荐答案

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

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

这篇关于如何一次读取多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆