可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to create an 16 bit image. So i have written a code.
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)
But whenever i run this code, i get an error displaying "TypeError: Image data can not convert to float".Is there any solution for this.
I apologies for the mistakes in my write up, as this is my first question which i have asked above.
回答1:
This happened for me when I was trying to plot an imagePath, instead of the image itself. The fix was to load the image, and plotting it.
回答2:
This question comes up first in the Google search for this type error, but does not have a general answer about the cause of the error. The poster's unique problem was the use of an inappropriate object type as the main argument for plt.imshow()
. A more general answer is that plt.imshow()
wants an array of floats and if you don't specify a float
, numpy, pandas, or whatever else, might infer a different data type somewhere along the line. You can avoid this by specifying a float
for the dtype
argument is the constructor of the object.
See the Numpy documentation here.
See the Pandas documentation here
回答3:
Try this
>>> plt.imshow(im.reshape(im.shape[0], im.shape[1]), cmap=plt.cm.Greys)
回答4:
From what I understand of the scikit-image docs (http://scikit-image.org/docs/dev/index.html), imshow() takes a ndarray as an argument, and not a dictionary:
http://scikit-image.org/docs/dev/api/skimage.io.html?highlight=imshow#skimage.io.imshow
Maybe if you post the whole stack trace, we could see that the TypeError comes somewhere deep from imshow().
回答5:
try
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()