TypeError: Image data can not convert to float

267,886

Solution 1

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

Solution 2

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.

Solution 3

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"

Solution 4

First read the image as an array

image = plt.imread(//image_path)
plt.imshow(image)

Solution 5

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.

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.

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()
Share:
267,886

Related videos on Youtube

Shubham Chahal
Author by

Shubham Chahal

Updated on July 09, 2022

Comments

  • Shubham Chahal
    Shubham Chahal almost 2 years

    I am trying to create a 16-bit image like so:

    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 I get the error TypeError: Image data can not convert to float.

    • rayryeng
      rayryeng over 8 years
      A is a dictionary, yet you are assuming that it's an image type for display. That's why you're getting the TypeError. However, I'm very confused because I don't know which image library you're using. You've imported scikit-image yet you tagged your post as using PIL. In addition, the imshow call is ambiguous because I don't know which package that comes from. None of your import statements makes that clear to me. Please edit your question to address what package imshow comes from and which image library you'd like to use for your post. BTW, images are indexed starting at 0.
  • Elm662
    Elm662 almost 5 years
    I have the same issue however I am working with RDD, my code works with python 2 but not with python 3, get the same error TypeError: Image data can not convert to float what should I do in this case? I cannot specify dtype for an rdd
  • Itamar Mushkin
    Itamar Mushkin about 4 years
    Putting an import statement in a loop is a very bad habit. I edited it for you.
  • Maheep
    Maheep about 4 years
    Hmm, I didn't realize it yet but now when you have pointed it out I kinda got the reason why you are saying so. Thank you anyways
  • WestCoastProjects
    WestCoastProjects about 4 years
    nice guess! trying this out
  • Frank HN
    Frank HN about 4 years
    You just need to first read the image, which was the case in my situation. make sure you read the path first. imread(path)
  • David Scott
    David Scott over 3 years
    As this is an older question with a lot of answers, could you provide information on why this answer is better than the many before it, some of which contain signficantly more explanation?
  • Kay
    Kay over 2 years
    This is actually the best answer. 👌🏼
  • Jan Wilamowski
    Jan Wilamowski about 2 years
    Please don't post important information like code and error messages as images
  • Muhammad Mohsin Khan
    Muhammad Mohsin Khan about 2 years
    Please write the code snippet instead of using images.
  • Timothy L.J. Stewart
    Timothy L.J. Stewart about 2 years
    wow, best answer for my problem. Now the question is why imshow doesn't have this default behavior when passed a path