2015-09-21 40 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 

所有現在工作得很好。 希望這會幫助別人,我理解了很長時間了!

相關問題