TypeError: Image data can not convert to float

PythonImageImage ProcessingPython Imaging-Library

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

First read the image as an array

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

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

Try to use this,

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

instead of plt.imshow(A)

Solution 8 - Python

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

Solution 9 - Python

Try this

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

It would help in some cases.

Solution 10 - Python

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

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

Solution 11 - Python

In my case image path was wrong! So firstly, you might want to check if image path is correct :)

Solution 12 - Python

Or maybe the image path contains Chinese characters, changing to English characters will solve this question.

enter image description here

enter image description here

Solution 13 - Python

As for cv2 is concerned.

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

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

Solution 14 - Python

The problem was that my array was in type u3 i changed it to float and it worked for me . I had a dataframe with Image column having the image/pic data.Reshaping part depends to person to person and image they deal with mine had 9126 size hence it was 96*96.

a = np.array(df_train.iloc[0].Image.split(),dtype='float')
a = a.reshape(96,96)
plt.imshow(a)

Solution 15 - Python

Input should be array

plt.imshow(plt.imread('image_path'))

Solution 16 - Python

This happened because you may transfer a wrong type to imshow(), for example I use albumentations.Compose to change image, and the result is a dict rather than numpy.ndarray. so just change

plt.imshow(cv2.cvtColor(aug(image=img), cv2.COLOR_BGR2RGB))

to

plt.imshow(cv2.cvtColor(aug(image=img)['image'], cv2.COLOR_BGR2RGB))

then it works.

Solution 17 - Python

I had the same problem and it turned out that my file path is not correct. Make sure you doublecheck your caps lock and spellings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShubham ChahalView Question on Stackoverflow
Solution 1 - PythoncometView Answer on Stackoverflow
Solution 2 - PythonmastDrinkNimbuPaniView Answer on Stackoverflow
Solution 3 - PythonspurthiView Answer on Stackoverflow
Solution 4 - PythonYash KhasgiwalaView Answer on Stackoverflow
Solution 5 - PythonMaheepView Answer on Stackoverflow
Solution 6 - PythonMichiel OvertoomView Answer on Stackoverflow
Solution 7 - PythonblitzorView Answer on Stackoverflow
Solution 8 - PythonntgView Answer on Stackoverflow
Solution 9 - PythonGoingMyWayView Answer on Stackoverflow
Solution 10 - PythonHong ChengView Answer on Stackoverflow
Solution 11 - PythonScottView Answer on Stackoverflow
Solution 12 - Pythonxuxu liView Answer on Stackoverflow
Solution 13 - PythonAnku5hkView Answer on Stackoverflow
Solution 14 - PythondevanshView Answer on Stackoverflow
Solution 15 - PythonSrinath KalikivayiView Answer on Stackoverflow
Solution 16 - PythonLividView Answer on Stackoverflow
Solution 17 - PythonabbujaansboyView Answer on Stackoverflow