开发者

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 suppose开发者_如何转开发d 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


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


jd already gave you the solution, but you didn't read it carefully ;)

This is incorrect:

subprocess.call( ['montage', '-tile 9x6', '-geometry +0+0', '*.png', 'out.png'] )

This is correct:

subprocess.call( ['montage', '-tile', '9x6', '-geometry', '+0+0', '*.png', 'out.png'] )


subprocess.call expects the entire command to be split into a list (with each argument as a separate element of the list). Try:

import shlex
command = "montage"
args = "-tile {}x{} -geometry +0+0 \"*.png\" out.png".format( width, height)
subprocess.call( shlex.split('{} {}'.format(command, args)) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜