当在python脚本中通过matlab中的操作系统命令调用时,Wand Python模块似乎不起作用

时间:2015-09-21 16:30:26

标签: python matlab wand

在matlab脚本中,我使用shell转义字符“!”运行其他python脚本,如外部命令。 所有都运行没有任何问题,例外,因为添加了一些关于模块Wand的代码(我需要这个将images.pdf转换为images.png和裁剪边距)。 这是令人难以置信的,它不是从matlab工作,但如果从shell发射它的工作非常好!

从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')
... 
>>> 

从脚本中,它运行良好:

- 脚本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
$

来自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的“转换”功能。

- 在shell中,正常工作:

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

: - (

1 个答案:

答案 0 :(得分:0)

我刚刚找到解决方案!一切都是因为用户$ PATH在Matlab中不一样......

示例,在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
$

但是在Matlab中:

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

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

2个解决方案:

- 从Matlab中的当前PATH开始:

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

- 或者完全定义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

现在一切都很顺利。 希望这会对某人有所帮助,我在理解之前已经工作了很长时间!!!