Search code examples
pythonimagemagickanimated-gif

Creating a gif in python using ImageMagick


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.


Solution

  • 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.