2015-09-21 37 views
0

在matlab脚本中,我使用shell转义字符“!”运行其他Python脚本,如外部命令。 所有运行都没有任何问题,除了添加了一部分关于模块Wand的代码(我需要将images.pdf转换为images.png和裁剪边距)。 这是令人难以置信的,它不是从matlab工作,但是如果它是从shell启动的,它工作得非常好!在Python脚本中调用matlab中的操作系统时,Wand Python模块似乎无法工作


从Python解释器,它工作正常:

:~ $ 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') 
... 
>>> 

从脚本,它工作正常:

-The脚本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') 

- 在一个shell中调用:

$ /Users/toto/test.py 
$ 

从MATLAB,不工作:

>> ! /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 
>> 

Arghhh我感觉像笼子里的狮子。我想我已经忘记了一些东西,但我没有找到! 任何帮助/建议将非常感谢!

编辑1:

看来,问题来自ImageMagick的的“转换”功能。

- 在一个外壳,做工精细:

$ /usr/local/bin/convert /Users/toto/test.pdf -crop 510x613+42+64 /Users/toto/test-crop.png 
$ 

- 在Matlab的,不工作:

>>! /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. 
>> 

:-(

回答

0

我只是找到了解决办法是所有引起因为用户$ PATH在Matlab中不一样......

实施例,在一个外壳:

$ echo $PATH 
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11/bin:/usr/bin/IRMAGE_python_scripts 
$ 

但在Matlab:

>> ! echo $PATH 
/usr/bin:/bin:/usr/sbin:/sbin 
>> 

的解决方案是在Matlab定义正确PATH(除用户$ PATH相同,在该系统):

2解决方案:

从目前PATH -Starting在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 
>> 

-Or定义完全的路径:

>> !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 

所有现在工作得很好。 希望这会帮助别人,我理解了很长时间了!

相关问题