尝试运行简单的 PIL python 示例,无法将 jpeg 转换为浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14543015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to run simple PIL python example, can't convert jpeg to float
提问by shosh
so I wrote a simple python script
所以我写了一个简单的python脚本
from PIL import Image
from pylab import *
im = array(Image.open('sample.jpg'))
imshow(im)
and i get this error from IDLE
我从 IDLE 收到这个错误
Traceback (most recent call last):
File "/home/michael/Dropbox/OpenCV/greyscale.py", line 5, in <module>
imshow(im)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2722, in imshow
imlim=imlim, resample=resample, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 7091, in imshow
im.set_data(X)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 418, in set_data
raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float
basically says i can not covert my image to float, anyone know what im missing
基本上说我不能把我的形象隐藏起来,任何人都知道我错过了什么
回答by Eric Olson
What you have is close.
Looking at the imshow docs, you need to pass either an Image OR a data array.
This should work:
你所拥有的很接近。查看 imshow 文档,您需要传递图像或数据数组。
这应该有效:
from PIL import Image
from pylab import *
im = Image.open('sample.jpg')
imshow(im)
Here's what imshow() is expecting:
这是 imshow() 所期待的:
"X may be a float array, a uint8 array or a PIL image."
“X 可能是一个浮点数组、一个 uint8 数组或一个 PIL 图像。”
There are some more details here:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow
这里有更多细节:http:
//matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow
回答by Stephen
I know this is coming very later but I thought I should answer just in case anyone else had the same problem. I ran into the same problem and the issue was the image I loaded did not exist in the project folder.so you need to check that sample.jpgexist and that it loads properly before using it.
我知道这会很晚才出现,但我想我应该回答,以防其他人遇到同样的问题。我遇到了同样的问题,问题是我加载的图像在项目文件夹中不存在。所以你需要检查它是否sample.jpg存在并且在使用它之前它加载正确。
if os.path.isfile('sample.jpg'): im = array(Image.open('sample.jpg'))
if im == None or im.size == 0:
print 'Image loaded is empty'
sys.exit(1)
回答by GoingMyWay
Try this
尝试这个
>>> import matplotlib.pyplot as plt
>>> plt.imshow(im.reshape(im.shape[0], im.shape[1]), cmap=plt.cm.Greys)

