![](/img/trans.png)
[英](/bin/bash: python3: command not found) When running python script through system command in Matlab
[英]Wand Python module does not seem to work when called in a python script through a command to the operating system in matlab
在matlab腳本中,我使用了外殼轉義字符“!”。 運行其他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')
-在外殼中調用:
$ /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
>>
啊,我感覺像是一只籠中的獅子。 我想我忘記了一些東西,但是我找不到! 任何幫助/建議將不勝感激!
看來問題出在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.
>>
:-(
我才找到解決方案! 都是由於用戶$ 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
現在一切都很好。 希望這會對某人有所幫助,在了解之前我已經工作了很長時間!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.