2

I have a bash script that has calls to the "convert" command in ImageMagick. This script works fine when I execute it in the terminal manually. Clearly I have the path to the 'convert' command added to my system path. When I try to execute it using a another script(written in python) , I run into an error the 'convert' is not found. Here is a simple example to reproduce the issue. I am on OSX Maverick.


Shell script: convert_svg_to_png.sh

#!/bin/bash
for filename in *.svg; do

        convert "$filename" "`basename $filename .svg`.png"

done

Python script: runme.py

import subprocess

subprocess.call('./convert_svg_to_png.sh')

Error: ./convert_svg_to_png.sh: line 5: convert: command not found


Any idea why this error?

Edit: When the bash script is modified to include the whole path to the convert function. This new error pops up:

dyld: Library not loaded: /ImageMagick-6.8.9/lib/libMagickCore-6.Q16.2.dylib Referenced from: %FullPath%convert Reason: image not found

The shell being called by python is /bin/bash. Same as in my terminal.

Mukund Raj
  • 19
  • 6
  • In the shell you are using to run python, what happens when you type `which convert` – lemonhead Aug 24 '15 at 23:49
  • also what is your `$PATH` set to in that shell – lemonhead Aug 24 '15 at 23:49
  • and you are sure if you run the shell script directly from that same shell, it works? – lemonhead Aug 24 '15 at 23:57
  • which python gives this: /Users/myusername/Desktop/libraries/ImageMagick-6.8.9/bin/convert . Also $PATH includes this folder: /Users/myusername/Desktop/libraries/ImageMagick-6.8.9/bin/ . It works when I run manually, I've also got the desired outputs. – Mukund Raj Aug 24 '15 at 23:58
  • hmmm, very weird, according to the `subprocess` docs, the default behavior is to inherit the current process environment... – lemonhead Aug 25 '15 at 00:01

2 Answers2

1

A few things...

  1. Ensure that the convert utility is reachable by your $PATH environment variable.

    export PATH="${PATH}:/Users/myusername/Desktop/libraries/ImageMagick-6.8.9/bin"
    
  2. If ImageMagick's ecosystem lives in a special place, set environment $MAGICK_HOME; such that, the runtime libraries can find everything.

    export MAGICK_HOME=/Users/myusername/Desktop/libraries/ImageMagick-6.8.9
    
  3. When invoking subprocess.call, set the keyword argument shell=True.

    import subprocess
    
    subprocess.call('./convert_svg_to_png.sh', shell=True)
    
emcconville
  • 23,800
  • 4
  • 50
  • 66
1

Ok then first try to verify if the command 'convert' is install on your machine. Which I think you do.

which convert

python program

import subprocess

command = 'bash convert_svg_to_png.sh'.split()

try:
    result = subprocess.check_call(command)
    print('successful')
except subprocess.CalledProcessError:
    print('An error occurs')

if you are using python3 then you can use shutil.which which will find the system path of convert or return None

python 3

import shutil
import subprocess

result = shutil.which('convert')

if result:
    command = 'bash convert_svg_to_png.sh'.split()
    try:
        result = subprocess.check_call(command)
        print('successful')
    except subprocess.CalledProcessError:
        print('An error occurs')

You could also use wand package which is a wrapper around Imagemagick http://docs.wand-py.org/en/0.4.1/guide/write.html#convert-images-to-jpeg or you could go to that post to see if there is a better approach https://stackoverflow.com/a/19718153/2581266

Community
  • 1
  • 1
Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101