10

I am trying to make a GIF-animation in R. I have an array of matrices which i wish to convert into a GIF animation. My strategy is inspired from this example:

http://ryouready.wordpress.com/2010/11/21/animate-gif-images-in-r-imagemagick/

where the following code produces 11 PNG-Pictures with the "png"-function in R. Next it calls for the external ImageMagick-program "convert" to compile the GIF animation.

dir.create("examples")
setwd("examples")

# Animated countdown from 10 to "GO!".
png(file="example%02d.png", width=200, height=200)
for (i in c(10:1, "G0!")){
plot.new()
text(.5, .5, i, cex = 6)
}
dev.off()

# convert the .png files to one .gif file using ImageMagick. 
system("convert -delay 80 *.png example_1.gif")
#shell("convert -delay 80 *.png example_1.gif")

The problem is that R doesn't seem to finde the exe-file "convert" which is a part of ImageMagick and installed on the C-drive (C:\Program Files\ImageMagick-6.8.5-Q16). In the comments to the website i am linking to earlier, it is suggested for Windows users to use "shell" instead of "system" to run external programs but none of the two work. The error message is

Invalid parameter - 80
Warning message:
running command 'convert -delay 80 *.png example_1.gif' had status 4

I've tried to change the Windows PATH enviroment variable in the systems properties, as suggested in this answer, but the PATH-variable was allready corectlly defined on my system. I also tried specifying the whole string of the convert.exe file, but also without luck...

How can i get ImageMagick to run through R?

Specs: Windows 7 Servicepack 1, R 3.0.0

Thanks in advance...

Community
  • 1
  • 1
Duffau
  • 471
  • 5
  • 15
  • The error message you quote is not indicating that R is not finding `convert`... What error message do you get when you use the full path? One possibility is that you have another `convert` program in your `PATH`. In case you added the directory at the end of the `PATH` environment variable, try putting it at the beginning instead. – flodel May 01 '13 at 02:17
  • What does `system("where convert", intern = TRUE)` give you? – flodel May 01 '13 at 02:35
  • 1
    The very first filepath in the `convert` variable is the ImageMagick filepath, just checked again. When i run the code `system("C:/Program Files/ImageMagick-6.8.5-Q16/convert -delay 80 *.png example_1.gif")` apparently nothing happens. When i run ´shell("C:/Program Files/ImageMagick-6.8.5-Q16/convert -delay 80 *.png example_1.gif")´ i get the error message: `'C:/Program' was not recognized as an internal or external command, program or batch file.` and then a whole series of R-errors. – Duffau May 01 '13 at 17:08
  • The code you suggest give the following result: `> system("where convert", intern = TRUE) [1] "C:\\Program Files\\ImageMagick-6.8.5-Q16\\convert.exe" [2] "C:\\Windows\\System32\\convert.exe"` Thank you very much for you help... it's greatly apprieciated. – Duffau May 01 '13 at 17:09
  • Try `system("C:/Program Files/ImageMagick-6.8.5-Q16/convert -delay 80 *.png example_1.gif", intern = TRUE)` – flodel May 01 '13 at 17:12
  • While "apparently nothing happens", was the file `example_1.gif` created? – flodel May 01 '13 at 17:15
  • No the GIF was not created. I have type it the windows searchbar to check it didn't appear somwhere else, but no result. – Duffau May 01 '13 at 17:18
  • I get the error: `> system("C:/Program Files/ImageMagick-6.8.5-Q16/convert -delay 80 *.png example_1.gif", intern = TRUE) Error in system("C:/Program Files/ImageMagick-6.8.5-Q16/convert -delay 80 *.png example_1.gif", : 'C:/Program' not found` – Duffau May 01 '13 at 17:20
  • 5
    maybe `system('"C:\\Program Files\\ImageMagick-6.8.5-Q16\\convert.exe" -delay 80 *.png example_1.gif', intern = TRUE)` then. – flodel May 01 '13 at 17:41
  • Thanks a lot! That worked! Do you have any idea what went wrong originaly? What does `intern=TRUE` mean? – Duffau May 01 '13 at 18:31
  • I'm still not sure. Does `system('convert -delay 80 *.png example_1.gif', intern = TRUE)` work? Also try `convert.exe`. `intern = TRUE` is just so it captures the output, so you have more info to work with whether it works or not. – flodel May 01 '13 at 18:42
  • Both yield the same error: `> system('convert.exe -delay 80 *.png example_1.gif', intern = TRUE) [1] "Invalid parameter - 80" attr(,"status") [1] 4 Warning message: running command 'convert.exe -delay 80 *.png example_1.gif' had status 4` – Duffau May 01 '13 at 19:00

4 Answers4

7

On Windows, there are several convert.exe commands, all of which are in the PATH. So you must specify the path to the right convert.exe executable. In my case, I had it in the LyX folder (however, you will find it in the ImageMagick installation too). Be careful with the quotes, the backslashes and the spacing if you are pasting. E.g. from within R:

system('"C:\\Program Files (x86)\\LyX 2.0\\imagemagick\\convert.exe" -delay 20 -loop 0 files_*.png animation.gif')
Federico Giorgi
  • 10,495
  • 9
  • 42
  • 56
  • Actually since this answer the command in magick changed to avoid clash with sytem32. Just use: system (magick convert -delay 40 -loop 0 *.png example_1.gif") and do not forget to specify your path in cd if your target are in a folder that is not your working directory system ("cd YOUR PATH") – Xochitl C. Aug 05 '19 at 11:40
3

I'm a windows 10 user, after defining the working directory I got it working in R using

shell("convert -set delay 80 -loop 0 *.jpg example_shell_test.gif")

within cmd each command means the following

convert = open convert function from ImageMagick

-set delay x = set the delay time between each frame to x (1000 = 1 second)

-loop 0 = loop forever, if set to 1 it will go through the images once

*.[image type]= and file of .[image type]

[name of output gif].gif = save new .gif as

I got it working first within command prompt by navigating to the directory and running the line

convert -set delay 80 -loop 0 *.jpg example_cmd_test.gif

Before this I was using -delay = 80 rather than -set delay 80 in cmd and got error: convert: invalid argument for option '-delay': = @ error/convert.c/ConvertImageCommand/1277.

In R using the system() command with the correct "-set delay x" I was getting error:

> system("convert -set delay = 80 -loop 0 *.jpg example_3.gif")
Invalid Parameter - delay
Warning message:
running command 'convert -set delay = 80 -loop 0 *.jpg example_3.gif' had status 4 

other errors in shell()

> shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif")
convert: unable to open image '80': No such file or directory @ error/blob.c/OpenBlob/3094.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509.
Warning messages:
1: running command 'C:\WINDOWS\system32\cmd.exe /c convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif' had status 1 
2: In shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif") :
  'convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif' execution failed with error code 1

I ran it in R with shell() after and it seems to work fine

shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test.gif")

Do have a look at this thread also

ImageMagick - Issue with Windows and convert function

Community
  • 1
  • 1
0

I know someone else already found the solution to your problem but there is an easier way to solve it without having to include the entire pathway in system(). Simply setani.options(convert = 'pathway/convert.exe') after loading the animation package.

0

After attempting all of these fixes as well as these and these to no success, I used alternative software to make the conversion, several of which are described here. I am a Windows user and found the simple instructions contained in that site for VirtualDub quickly accomplished this task.

Community
  • 1
  • 1
Luke Macaulay
  • 393
  • 5
  • 14