Python 类型错误:图像数据无法转换为浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32302180/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:22:03  来源:igfitidea点击:

TypeError: Image data can not convert to float

pythonimageimage-processingpython-imaging-library

提问by Shubham Chahal

I want to create an 16 bit image. So i have written a code.

我想创建一个 16 位图像。所以我写了一个代码。

     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.

我为我写的错误道歉,因为这是我在上面提出的第一个问题。

回答by Michiel Overtoom

From what I understand of the scikit-image docs (http://scikit-image.org/docs/dev/index.html), imshow() takes a ndarrayas an argument, and not a dictionary:

根据我对 scikit-image 文档(http://scikit-image.org/docs/dev/index.html)的理解, imshow() 将ndarray作为参数,而不是字典:

http://scikit-image.org/docs/dev/api/skimage.io.html?highlight=imshow#skimage.io.imshow

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().

也许如果您发布整个堆栈跟踪,我们可以看到 TypeError 来自 imshow() 深处的某个地方。

回答by ntg

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()

回答by GoingMyWay

Try this

尝试这个

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

It would help in some cases.

在某些情况下它会有所帮助。

回答by mastDrinkNimbuPani

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.

当我尝试绘制 imagePath 而不是图像本身时,这发生在我身上。解决方法是加载图像并绘制它。

回答by comet

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 floatfor the dtypeargument is the constructor of the object.

这个问题首先出现在 Google 搜索此类型错误中,但没有关于错误原因的一般答案。发布者的独特问题是使用不适当的对象类型作为 的主要论据plt.imshow()。一个更一般的答案是需要plt.imshow()一个浮点数组,如果你没有指定 a float、numpy、pandas 或其他任何东西,可能会推断出不同的数据类型。您可以通过floatdtype参数指定 a 来避免这种情况是对象的构造函数。

See the Numpy documentation here.

请参阅此处Numpy 文档

See the Pandas documentation here

请参阅此处Pandas 文档

回答by spurthi

The error occurred when I unknowingly tried plotting the image path instead of the image.

当我在不知不觉中尝试绘制图像路径而不是图像时发生错误。

My code :

我的代码:

import cv2 as cv
from matplotlib import pyplot as plt
import pytesseract
from resizeimage import resizeimage

img = cv.imread("D:\TemplateMatch\fitting.png") ------>"THIS IS THE WRONG USAGE"
#cv.rectangle(img,(29,2496),(604,2992),(255,0,0),5)
plt.imshow(img)

Correction: img = cv.imread("fitting.png")--->THIS IS THE RIGHT USAGE"

更正: img = cv.imread("fitting.png")--->这是正确的用法”

回答by xor

Try to use this,

尝试使用这个,

   plt.imshow(numpy.real(A))
   plt.show()

instead of plt.imshow(A)

代替 plt.imshow(A)

回答by Hong Cheng

I guess you may have this problem in Pycharm. If so, you may try this to your problem.

我猜你在 Pycharm 中可能有这个问题。如果是这样,你可以试试这个解决你的问题。

Go to File-Setting-Tools-Python Scientificin Pycharm and remove the option of Show plots in tool window.

转到File-Setting-Tools-Python ScientificPycharm 并删除Show plots in tool window.

回答by Maheep

I was also getting this error, and the answers given above says that we should upload them first and then use their name instead of a path - but for Kaggle dataset, this is not possible.

我也遇到了这个错误,上面给出的答案说我们应该先上传它们,然后使用它们的名称而不是路径 - 但对于 Kaggle 数据集,这是不可能的。

Hence the solution I figure out is by reading the the individual image in a loop in mpimg format. Here we can use the path and not just the image name.

因此,我想出的解决方案是以 mpimg 格式循环读取单个图像。在这里我们可以使用路径而不仅仅是图像名称。

I hope it will help you guys.

我希望它会帮助你们。

import matplotlib.image as mpimg
for img in os.listdir("/content/train"): 
  image = mpimg.imread(path)
  plt.imshow(image)
  plt.show()

回答by Anku5hk

As for cv2 is concerned.

至于cv2而言。

  1. You might not have provided the right file type while cv2.imread(). eg jpg instead of png.
  2. Or you are providing image path instead of image's array. eg plt.imshow(img_path),
  1. 您可能没有提供正确的文件类型,而cv2.imread(). 例如 jpg 而不是 png。
  2. 或者您提供的是图像路径而不是图像的数组。例如plt.imshow(img_path)

try cv2.imread(img_path)first then plt.imshow(img)or cv2.imshow(img).

cv2.imread(img_path)先尝试然后plt.imshow(img)cv2.imshow(img)