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")
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With