Calling ImageMagick's convert

Chris Rebert clp at rebertia.com
Mon Dec 22 03:33:56 EST 2008


On Mon, Dec 22, 2008 at 12:15 AM, cmr.Pent at gmail.com <cmr.Pent at gmail.com> wrote:
> Hello group!
>
> I'm a Python beginner. I'm trying to call ImageMagick's convert
> program from my code.
> My OS is Debian testing, and my version of Python is 2.5.
>
> I've just stumbled upon a problem:
>
> (1) subprocess.call('convert in.png -resize 640x480 out.png', shell =
> True)
>
> works, but none of the following does:
>
> (2) subprocess.call('convert in.png -resize 640x480 out.png')
> (3) subprocess.call(['convert', 'in.png', '-resize 640x480',
> 'out.png'])

I think this needs to be:

subprocess.call(['convert', 'in.png', '-resize', '640x480', 'out.png'])

Otherwise, it gets '-resize 640x480' as a single escaped option when
it's really 2 options, which is the error message you're getting.
You have to split the arguments up just like the shell would, which
basically means at whitespace unless quoting is used, which is not the
case here.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com

> (4) subprocess.call(['convert', 'in.png', '-resize 640x480',
> 'out.png'], shell = True)
>
> In these cases, convert program says 'unrecognized option -resize
> 640x480'.
> At the same time,
>
> subprocess.call(['ls', '-l'])
>
> works as expected.
>
> I'd like to use variant (3), as it seems the most handy. I don't
> understand why is shell = True required in case of convert. Is it a
> bug in Python, in ImageMagick, or am I missing something very basic
> here?
> Any hint appreciated.
>
> Andrey
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list