问题
I am testing scipy.misc.imshow and I got RuntimeError: Could not execute image viewer.
I am using Python3.4 and running it on CentOS 7.
import scipy.misc
img = scipy.misc.imread('Data/cat.jpg')
assert len(img.shape) == 3
img_resized = scipy.misc.imresize(img, (224, 224))
img_answer = (img_resized/255.0).astype('float32')
scipy.misc.imshow(img_answer)
And I got an error:
sh: see: command not found
Traceback (most recent call last):
File "/usr/local/pycharm/helpers/pydev/pydev_run_in_console.py", line 71, in <module>
globals = run_file(file, None, None)
File "/usr/local/pycharm/helpers/pydev/pydev_run_in_console.py", line 31, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "/usr/local/pycharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/root/PycharmProjects/myVQA/testgood.py", line 6, in <module>
scipy.misc.imshow(img_answer)
File "/usr/lib64/python3.4/site-packages/scipy/misc/pilutil.py", line 442, in imshow
raise RuntimeError('Could not execute image viewer.')
RuntimeError: Could not execute image viewer.
It says that the see
command is not found. Where is the see
command installed on CentOS7? How can I fix the problem?
I tried to add SCIPY_PIL_IMAGE_VIEWER=/bin/eog
to /etc/profile
but it seems to be no help.
回答1:
You can use matplotlib.pyplot as an alternative to using SciPy's imshow method.
import scipy.misc
img = scipy.misc.imread('Data/cat.jpg')
assert len(img.shape) == 3
img_resized = scipy.misc.imresize(img, (224, 224))
img_answer = (img_resized/255.0).astype('float32')
import matplotlib.pyplot as plt
plt.imshow(np.uint8(img_tinted))
plt.show()
=>
P.S. If we directly plot the image as plt.imshow(img_tinted)
, then it might sometimes give weird results if the data presented to it is not in the form of unit8. Hence, to prevent this we explicitly cast np.uint8
to the image such as plt.imshow(np.uint8(img_tinted))
The following images show output in the absence of np.uint8
回答2:
I solved my problem by:
1 adding the following to /etc/profile
SCIPY_PIL_IMAGE_VIEWER=/bin/eog
export SCIPY_PIL_IMAGE_VIEWER
2 reboot
if you do not export SCIPY_PIL_IMAGE_VIEWER,
cmd = os.environ.get('SCIPY_PIL_IMAGE_VIEWER', 'see')
will not recognize SCIPY_PIL_IMAGE_VIEWER
回答3:
The following also works for me (much lighter and faster than using matplotlib):
from os import environ
environ['SCIPY_PIL_IMAGE_VIEWER'] = {your image viewer pgm} # e.g. C:/IrfanView/i_view32.exe
I can then use scipy's imshow().
来源:https://stackoverflow.com/questions/40229630/scipy-misc-imshow-runtimeerrorcould-not-execute-image-view