2

From the python console this works:

convert -quality 100 in.pdf out.png

but when I add that command to my Python script like this:

Popen(['convert', '-quality 100', 'in.pdf', 'out.png'])

I get:

unrecognized option `-quality 100'

If I change that parameter to '-quality=100' I still get the error.

I tried fixing it like this:

Popen(['convert', '-quality', '100', 'in.pdf', 'out.png'])

which runs but fails to produce an out.png.

UPDATE: The last version is working. I must have mistyped it originally.

3
  • 1
    Your last example should work, does it give any output? Commented Jul 28, 2011 at 18:18
  • 1
    Yes it's working now. I must have mistyped it the first time. Commented Jul 28, 2011 at 18:24
  • @ryaz: "The last version is working". Either answer your own question (that's okay) or close the question. Commented Jul 28, 2011 at 18:30

3 Answers 3

5

Every argument gets its own list element, so the second variant is correct.

You should bear in mind that until a call to communicate finishes, the command may still run (although that's unlikely in your case). Check returncode after calling communicate to find out whether the program encountered any errors (like a malformed PDF file or so).

Also, imagemagick convert writes out multipage PDFs to multiple PNG files (out-0.png, out-1.png). Check whether those exist. Use -append to supress that behavior.

import subprocess
Popen = subprocess.Popen
s = Popen(['convert', '-quality', '100', 'in.pdf', '-append', 'out.png'])
s.communicate()
if s.returncode != 0:
   raise OSError('convert error')
Sign up to request clarification or add additional context in comments.

Comments

2

This works fine:

#! /usr/bin/python3.2

from subprocess import Popen

Popen ( ['convert', '-quality', '100', 'test.pdf', 'out.png'] )

Using

Version: ImageMagick 6.6.2-6 2011-03-16 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP 

Are you sure the script can find your in.pdf?

Comments

1

The convert command must be found in the execution path when the script runs. Can you include the full path to convert in the arguments?

Popen(['/path/to/convert', '-quality', '100', 'in.pdf', 'out.png'])

Replace '/path/to/convert' with the real path. Also, you will need to ensure that the account that executes the script has read and write permissions in the current directory.

3 Comments

I think hard coding a reference to convert is not a good idea. Hopefully these binaries will be in the path otherwise the program will not work on different systems.
@Whoopska: Notwithstanding "Hopefully", if the binary is not in the path, the code will not execute successfully. The ideal situation is to either a) determine if the command exists before attempting to execute it, or b) place the path to "convert" in a configuration file, and prompt the user to verify or change it during installation.
I can't testify for all operating systems, but it seems like error code 127 could be picked up with something like .returncode or as seems to be the case upon testing this, by catching the OSError. I agree with you though, that either of your alternatives is pretty ideal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.