2015-08-30 36 views
6

我想创建一个16位图像。所以我写了一个代码。TypeError:图像数据无法转换为浮点数

 import skimage 
    import random 
    from random import randint       
    xrow=raw_input("Enter the number of rows to be present in image.=>") 
    row=int(xrow) 
    ycolumn=raw_input("Enter the number of columns to be present in image.=>") 
    column=int(ycolumn) 

     A={} 
     for x in xrange(1,row): 
      for y in xrange(1,column): 
       a=randint(0,65535) 
       A[x,y]=a 

     imshow(A) 

但每当我运行这段代码,我得到显示错误“类型错误:图像数据无法转换为浮动”。就是有这方面的任何解决方案。

我对自己写的错误表示歉意,因为这是我上面提到的第一个问题。

+1

'A'是一本字典,但我们假定你是,它是用于显示的图像类型。这就是你得到'TypeError'的原因。不过,我很困惑,因为我不知道你使用的是哪个图像库。你已经导入了'scikit-image',但是你使用PIL标记了你的文章。另外,'imshow'调用是不明确的,因为我不知道哪个包是从哪里来的。没有任何“进口”声明对我来说很明显。请编辑您的问题,以解决'imshow'包的来源以及您想要用于发布的图像库。顺便说一句,图像索引从'0'开始。 – rayryeng

回答

0

尝试

import skimage 
import random 
from random import randint 
import numpy as np 
import matplotlib.pyplot as plt 


xrow = raw_input("Enter the number of rows to be present in image.=>") 
row = int(xrow) 
ycolumn = raw_input("Enter the number of columns to be present in image.=>") 
column = int(ycolumn) 

A = np.zeros((row,column)) 
for x in xrange(1, row): 
    for y in xrange(1, column): 
     a = randint(0, 65535) 
     A[x, y] = a 

plt.imshow(A) 
plt.show() 
1

试试这个

>>> plt.imshow(im.reshape(im.shape[0], im.shape[1]), cmap=plt.cm.Greys) 
4

这事对我来说,当我试图代替图像本身绘制的的ImagePath。解决的办法是加载图像,并绘制它。

0

这个问题首先出现在谷歌搜索这种类型的错误,但没有关于错误原因的一般答案。海报的独特问题是使用不适合的对象类型作为plt.imshow()的主要参数。更普遍的答案是,plt.imshow()想要一个浮点数组,如果你没有指定一个float,numpy,pandas或其他任何东西,可能会推断出沿线的不同数据类型。您可以通过指定float来避免这种情况,dtype参数是对象的构造函数。

查看Numpy documentation here

Pandas documentation here

相关问题