Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I image_read multiple images at once?

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")
like image 693
AJMA Avatar asked Apr 02 '18 13:04

AJMA


People also ask

How do you process multiple images in python?

You can resize multiple images in Python with the awesome PIL library and a small help of the os (operating system) library. By using os. listdir() function you can read all the file names in a directory. After that, all you have to do is to create a for loop to open, resize and save each image in the directory.

How do I read multiple images from a folder in Python?

At first, we imported the pathlib module from Path. Then we pass the directory/folder inside Path() function and used it . glob('*. png') function to iterate through all the images present in this folder.


1 Answers

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")
like image 165
Nova Avatar answered Oct 17 '22 08:10

Nova