2

I am trying to rasterize some fonts using imagemagick with this command which works fine from a terminal:

convert -size 30x40 xc:white -fill white -fill black -font "fonts\Helvetica Regular.ttf" -pointsize 40 -gravity South -draw "text 0,0 'O'" draw_text.gif

Running the same command using subprocess to automate it does not work:

try:
    cmd= ['convert','-size','30x40','xc:white','-fill','white','-fill','black','-font','fonts\Helvetica Regular.ttf','-pointsize','40','-gravity','South','-draw',"text 0,0 'O'",'draw_text.gif']
    #print(cmd)
    subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT)
except CalledProcessError as e:
    print(e)
    print(e.output)

.

Command '['convert', '-size', '30x40', 'xc:white-fill', 'white', '-fill', 'black', '-font', 'fonts\\Helvetica Regular.ttf', '-pointsize', '40', '-gravity', 'South', '-draw', "text 0,0 'O'", 'draw_text.gif']' returned non-zero exit status 4
b'Invalid Parameter - 30x40\r\n'
4
  • Why are you running it with shell = True ? Commented Feb 22, 2013 at 4:48
  • @gahooa For some reason, I have never been able to run anything without shell=True. I tried it with shell=False but that gives me the same error. Anyway, it is not unsafe because I am not using any untrusted input. Commented Feb 22, 2013 at 4:51
  • 1
    For starters, take str.join(' ', cmd) and copy+paste that to the command line to see if it still works. Commented Feb 22, 2013 at 4:54
  • @gahooa That works if I quote the fonts\Helve... and text 0,0 'O'. I think this is normal because the quotes are only there because of the space in the argument and the shell strips away the quotes before passing them to the program Commented Feb 22, 2013 at 4:59

1 Answer 1

9

I figured it out: It turns out that windows has its own convert.exe program in PATH.

The following code prints b'C:\\Windows\\System32\\convert.exe\r\n':

try:
    print(subprocess.check_output(["where",'convert'],stderr=subprocess.STDOUT,shell=True))
except CalledProcessError as e:
    print(e)
    print(e.output)

Running the same code in a terminal shows that imagemagick's convert shadows Windows' convert:

C:\Users\Navin>where convert                                                    
C:\Program Files\ImageMagick-6.8.3-Q16\convert.exe                              
C:\Windows\System32\convert.exe                                                 

.

I did not restart python after installing ImageMagick so its PATH still pointed to the Windows version.

Using the full path works:

try:
    cmd= ['C:\Program Files\ImageMagick-6.8.3-Q16\convert','-size','30x40','xc:white','-fill','white','-fill','black','-font','fonts\Helvetica Regular.ttf','-pointsize','40','-gravity','South','-draw',"text 0,0 'P'",'draw_text.gif']
    print(str.join(' ', cmd))
    print('stdout: {}'.format(subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT)))
except CalledProcessError as e:
    print(e)
    print(e.output)
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know why but in OSX(10.11) works only without shell=True.
@AndréDuarte Yep, I did the same thing 4 years ago when I asked this question. Though since then, I realized that ImageMagick is a piece of crap and I'd much rather use an image library with a real API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.