0

I am trying to create a gif using python either through the GraphicsMagick module or using os.system commands with the ImageMagick.exe. The most relevant question I have found here was this question about 6 years ago.

Similar Question 6 years old

URL for imagemagick download

file:///C:/Program%20Files/ImageMagick-6.9.1-Q16/www/binary-releases.html#windows

GraphicsMagick Download

Below is my code (not working). I am currently trying the ImageMagick route as the above question stated that this was more pythonic. Any help would be appreciated.

from pgmagick import Image, ImageList, Geometry, Color 
import os, sys
import glob

#dataDir = sys.argv[1]
#outDir  = sys.argv[2]

dataDir = 'C:\\Users\\name\\Desktop\\a'

os.chdir(dataDir)
fileList = glob.glob(dataDir + '\*.jpg')
fileList.sort()

os.system('convert fileList -delay 20 -loop 0 *jpg animated.gif')

I get an invalid syntax error on the last line of my code.

Community
  • 1
  • 1
Almidas
  • 379
  • 2
  • 6
  • 16
  • Surround the argument in quotes (`os.system('convert...gif')`). – TigerhawkT3 May 19 '15 at 19:03
  • Thanks. That is one mistake down. I still have the overall problem of not fully understanding the imagemagick commands. I now have an invalid parameter error. – Almidas May 19 '15 at 19:10
  • I think when [the guy in that question](http://stackoverflow.com/questions/753190/programmatically-generate-video-or-animated-gif-in-python#comment34815046_753832) said to use `convert -delay 20 -loop 0 *jpg animated.gif`, he meant, just paste it straight into the command line and don't bother using Python at all. That would save you a lot of time in wrestling with `system` etc. – Kevin May 19 '15 at 19:17
  • 2
    In any case, this reminds me of [my own experiments in executing ImageMagick from Python](http://pastebin.com/Vw3KuUj0). Maybe you'll find some useful insights in there. For example, I had better luck with `subprocess.call` than with `os.system`. Still not sure why though. – Kevin May 19 '15 at 19:24
  • I opened a command window in the directory with the jpg files and used that exact command. I still get an invalid parameter error. – Almidas May 19 '15 at 19:27
  • Thanks. I will get that a try. – Almidas May 19 '15 at 19:28

1 Answers1

0

For anyone that sees this in the future when trying to make a gif with imagemagick on a windows machine, this is the solution that I figured out. I don't know if this is the most efficient way in terms of memory, but it works.

import os, sys

dataDir = 'fullpath of directory with images'
os.chdir(dataDir)


os.system('SETLOCAL EnableDelayedExpansion')
#This is the path of the imagemagick installation convert command.  
#The invalid parameter I was  getting was because the computer was trying to 
#use a different convert command.  This code sets convert as a different
#variable and then calls that new variable as the convert command.  
os.system('SET IMCONV="C:\Program Files\ImageMagick-6.9.1-Q16\Convert"')
os.system('%IMCONV% *.jpg animated.gif')

Some changes I made to simplify the solution. Instead of making a file list as I did in my question, I just stuck all the images in a directory, and then called the command to convert these images to a gif. This is the simplest solution that I have come across.

Almidas
  • 379
  • 2
  • 6
  • 16