Search code examples
pythonparametersimagemagickglob

Python, ImageMagick and `subprocess`


I'm trying to assemble images with a call to ImageMagick's montage from a Python script like this:

 command = "montage"
 args = "-tile {}x{} -geometry +0+0 \"*.png\" out.png".format( width, height)
 sys.stdout.write( "  {} {}\n".format(command, args) )
 print subprocess.call( [command, args] )

However, montage only shows usage. If I run the command manually, everything works. ImageMagick is supposed to support filename globbing in Windows, so *.png is expanded. But apparently, this behaviour is suppressed by subprocess. Do I have to use glob to feed montage with a list of the filenames?

Further information Thanks so far. But even when I use:

command = "montage"
tile = "-tile {}x{}".format( width, height)
geometry = "-geometry +0+0"
infile = "*.png"
outfile = "out.png"
sys.stdout.write( "  {} {} {} {} {}\n".format(command, tile, geometry, infile, outfile) )
print [command, tile, geometry, infile, outfile]
#~ print subprocess.call( [command, tile, geometry, infile, outfile] )
print subprocess.call( ['montage', '-tile 9x6', '-geometry +0+0', '*.png', 'out.png'] )

I get an error:

 Magick: unrecognized option `-tile 9x6' @ error/montage.c/MontageImageCommand/1631.

I'm on Windows 7, ImageMagick 6.6.5-7 2010-11-05 Q16 http://www.imagemagick.org, Python 2.7


Solution

  • Instead of [command, args], you should pass ['montage', '-tile', '{}x{}'.format(...), '-geometry'...] as first argument. You might need shell=True as well.