Wand Python module (convert function, see Edit 1) does not seem to work when called in a python script through a command to the operating system in matlab.

조회 수: 2 (최근 30일)
Hello,
In a matlab script, I use the shell escape character "!" to run other python scripts like external commands. All was running without any problem, excepted since I added a portion of code concerning the module Wand (I need this to convert images.pdf to images.png and crop margins). It's incredible, it is not working from matlab but is working very well if it is launched from a shell !
From the Python interpreter, it is working fine:
$ python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from wand.image import Image as wandImage
>>> with wandImage(filename = '/Users/toto/test.pdf') as img:
... img.save(filename = '/Users/toto/test.png')
...
>>>
From a script, it is working fine:
-The script test.py:
$ pwd; ls -l test.py
/Users/toto
-rwxrwxrwx 1 toto staff 326 22 sep 10:23 test.py
$
$ more test.py
#! /usr/bin/python
# -*- coding: utf-8 -*- # Character encoding, recommended
## -*- coding: iso-8859-1 -*- # Character encoding, old (Latin-1)
from wand.image import Image as wandImage
with wandImage(filename = '/Users/toto/test.pdf') as img:
img.save(filename = '/Users/toto/test.png')
-Call in a shell:
$ /Users/toto/test.py
$
From Matlab, not working !:
>> ! /Users/toto/test.py
Traceback (most recent call last):
File "/Users/toto/test.py", line 9, in <module>
img.save(filename = '/Users/toto/test.png')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wand/image.py", line 2719, in save
self.raise_exception()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wand/resource.py", line 222, in raise_exception
raise e
wand.exceptions.WandError: wand contains no images `MagickWand-1' @ error/magick-image.c/MagickWriteImage/13115
>>
Edit 1:
It seems that the problem comes from the "convert" function of ImageMagick.
-In a shell, work fine:
$ /usr/local/bin/convert /Users/toto/test.pdf -crop 510x613+42+64 /Users/toto/test-crop.png
$
-In Matlab, does not work:
>>! /usr/local/bin/convert /Users/toto/test.pdf -crop 510x613+42+64 /Users/toto/test-crop.png
convert: no images defined `/Users/toto/test-crop.png' @ error/convert.c/ConvertImageCommand/3230.
>>
Any help / suggestion will be very appreciated !!! Best regards.

답변 (1개)

Eric Condamine
Eric Condamine 2015년 9월 22일
I just found the solution ! All was caused because the user $PATH is not the same in Matlab ......
Example, in a Shell:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11/bin:/usr/bin/IRMAGE_python_scripts
$
But in Matlab:
>> ! echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin
>>
The solution is to define in Matlab the correct PATH (the same than the user $PATH, in the system) :
2 solutions:
1. Starting from the current PATH in Matlab:
>> setenv('PATH', [getenv('PATH'),':','/usr/local/bin',':','/opt/X11/bin',':','/usr/texbin',':','/usr/X11/bin',':','/usr/bin/IRMAGE_python_scripts']);
>> ! echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11/bin:/usr/bin/IRMAGE_python_scripts
>>
2. Or defining totally the PATH:
>> !echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin
>> setenv('PATH', ['/usr/bin',':','/bin',':','/usr/sbin',':','/sbin',':','/usr/local/bin',':','/opt/X11/bin',':','/usr/texbin',':','/usr/X11/bin',':','/usr/bin/IRMAGE_python_scripts'])
>> !echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11/bin:/usr/bin/IRMAGE_python_scripts
All is working very well now.
Hoping this will help somebody, I have worked around a long time before understanding !!!

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by