在 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')
- 在 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
>>
啊,我感觉自己像一头笼中的狮子。我想我忘记了什么,但我没有找到!任何帮助/建议将不胜感激!!!
编辑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.
>>
:-(