Scott_Dayton
Scott_Dayton

Reputation: 9

I am trying to save 360 png files as a gif with image magick in R (I am working with MacOS)

please let me know any other system/code I need to include, as I am not as familiar with writing out images to my computer. I am creating 360 png files as follows:

for(theta in 1:360){
    ic=as.character(theta)
    if(theta<10) ic=paste("00",ic,sep="")
    if(theta>=10 & theta<100) ic=paste("0",ic,sep="") # make filenames the same length
    fn=paste("c:iris360\\HW4_",ic,".png",sep="") #filename
    png(fn,width=1000,height=1000) # save as *.png
    p3(X1,X2, r=100,theta=theta,mainL=paste("theta =",theta))
    # legend("topleft",pch=16,cex=1.5,col=allcl)
    dev.off()
}
system("magick c:iris360\\HW4*.png c:iris.gif") 

where p3 is just a function that takes my matrices X1 and X2 and plots the points and their segments(let me know if I need to include it as well). However, I get this error: magick: must specify image size iris360HW4*.png' @ error/raw.c/ReadRAWImage/140.

I am unable to open the gif file, as my mac says it is damaged or uses a file format that preview does not recognize.

Update 1: I replaced fn's declaration with

fn <- sprintf("c:iris360/HW4_%03i.png", theta)

as well as replacing ic with sprintf("%03i", theta) everywhere it appeared, but still got the same specify image size error.

When I run the system command into my terminal, I still get the same error asking me to specify the image size.

Upvotes: 0

Views: 285

Answers (1)

Karl Edwards
Karl Edwards

Reputation: 335

Magick needs to know several things (e.g., image size, delay between frames, images to use, destination file name) in order to convert a stack of png into a gif. See GIF Animations and Animation Meta-data

magick -delay 100  -size 100x100 xc:SkyBlue \
      -page +5+10  balloon.gif   -page +35+30 medical.gif  \
      -page +62+50 present.gif   -page +10+55 shading.gif  \
      -loop 0  animation.gif

So it looks like you need to change

system("magick c:iris360\\HW4*.png c:iris.gif")

to something more like

system("magick -delay 10 -size 100x100 —loop 0 c:iris360\\HW4*.png c:iris.gif")

Upvotes: 0

Related Questions