0

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 Answers2

0

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • I am still learning so apologies for the stupid question, but that doesn't look like R code to me. How can I do that function in R? (using that produces errors) – user14801595 Jan 06 '21 at 11:06
  • Sorry, that is command line code. Unfortunately, I do not know R or Rmagick. But your post never mentioned R and I did not notice the R: in your title. – fmw42 Jan 06 '21 at 19:12
0

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