0
votes

I have many images (plots) saved as png from another script:

  • x1.png
  • x2.png
  • x3.png
  • x4.png
  • y1.png
  • y2.png

etc. I am trying to make a gif of these, which I have done successfully in magick. However, this loops through 1 image per frame. I am trying to load 4 images at a time, and stitch them together on a panel, and create a gif of these panels.

I have successfully created a gif of these images. However, I want to load 4 images at a time, and then form these into a gif.

y <- (list.files(path='foo/bar/', pattern = '*1.png', full.names = TRUE))
plot(y[1]) # does nothing

How can I load or display 4 pngs at a time in one panel? I can do this usually by using:

par(mfrow=c(2,2))
plot(a)
plot(b)
plot(c)
plot(d)

however I am using magick to load pngs (of plots, but I obviously can't treat them as regular plots)

img1 <- magick::image_read("foo/bar/x1.png")
plot(img1) # does nothing
print(img1) # displays img 1 as a plot, and prints some details in the output window.

I'm sure I could rewrite my other code to display 4 plots, and save them as 1 png, but I am trying to write a small separate script to load these pre-existing images, and stitch the pngs together. Is this possible using par(mfrow=c(2,2))?

I'm sure I'm missing a great many simple things. Thank you all for your time.

2
Use montage in ImageMagick. See legacy.imagemagick.org/Usage/montage - fmw42
check out plot_grid() in the cowplot package - cory

2 Answers

0
votes

You can do that in ImageMagick using montage. For example

montage lena.jpg mandril3.jpg zelda1.jpg peppers.jpg -tile 2x2 -geometry +0+0 montage.gif

enter image description here

0
votes

To run ImageMagick commands (or any terminal code) in R, you just have to enclose it in quotes and pass that to the function system().

system("montage lena.jpg mandril3.jpg zelda1.jpg peppers.jpg -tile 2x2 -geometry +0+0 montage.gif")