TypeError: Image data can not convert to float
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()
Related videos on Youtube
Shubham Chahal
Updated on July 09, 2022Comments
-
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 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 theTypeError
. However, I'm very confused because I don't know which image library you're using. You've importedscikit-image
yet you tagged your post as using PIL. In addition, theimshow
call is ambiguous because I don't know which package that comes from. None of yourimport
statements makes that clear to me. Please edit your question to address what packageimshow
comes from and which image library you'd like to use for your post. BTW, images are indexed starting at0
.
-
-
Elm662 almost 5 yearsI 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 about 4 yearsPutting an import statement in a loop is a very bad habit. I edited it for you.
-
Maheep about 4 yearsHmm, 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 about 4 yearsnice guess! trying this out
-
Frank HN about 4 yearsYou 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 over 3 yearsAs 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 over 2 yearsThis is actually the best answer. 👌🏼
-
Jan Wilamowski about 2 yearsPlease don't post important information like code and error messages as images
-
Muhammad Mohsin Khan about 2 yearsPlease write the code snippet instead of using images.
-
Timothy L.J. Stewart about 2 yearswow, best answer for my problem. Now the question is why
imshow
doesn't have this default behavior when passed apath