Reputation: 834
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
.
Upvotes: 76
Views: 414192
Reputation: 380
For anyone still stuck with this error trying to open an image, my problem was that the image was an .heic file and for whatever reason PIL.Image could not open it.
You can try some of these solutions: How to work with HEIC image file types in Python
I used this library https://docs.wand-py.org/en/0.6.4/ and was able to open the image from s3 as a stream without modifying the image. You could also just open from disk, I was just happened to be using s3.
from io import BytesIO
from wand.image import Image
s3_resource = boto3.resource('s3')
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(your_bucket)
object = bucket.Object(your_key)
response = object.get()
file_stream = response['Body'].read()
image = Image(file=BytesIO(file_stream))
Upvotes: 0
Reputation: 21
This problem is due to the missing the parenthesis on the squeeze function.
plt.imshow(image.squeeze)
To solve the problem, add parenthesis to invoke the squeeze function.
plt.imshow(image.squeeze())
Upvotes: 0
Reputation: 69
For an image file in .mat format. I have done the following to show the image using the imshow() function.
mat = scipy.io.loadmat('point05m_matrix.mat')
x = mat.get("matrix")
print(type(x))
print(len(x))
plt.imshow(x, extent=[0,60,0,55], aspect='auto')
plt.show()
Upvotes: -2
Reputation: 9
Or maybe the image path contains Chinese characters, changing to English characters will solve this question.
Upvotes: 0
Reputation: 106
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.
Upvotes: 1
Reputation: 31
Input should be array
plt.imshow(plt.imread('image_path'))
Upvotes: -1
Reputation: 89
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)
Upvotes: -1
Reputation: 711
First read the image as an array
image = plt.imread(//image_path)
plt.imshow(image)
Upvotes: 24
Reputation: 321
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"
Upvotes: 28
Reputation: 5840
In my case image path
was wrong! So firstly, you might want to check if image path is correct :)
Upvotes: 0
Reputation: 887
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()
Upvotes: 5
Reputation: 39
As for cv2 is concerned.
cv2.imread()
.
eg jpg instead of png.plt.imshow(img_path)
,try cv2.imread(img_path)
first then plt.imshow(img)
or cv2.imshow(img)
.
Upvotes: -1
Reputation: 17468
Try this
plt.imshow(im.reshape(im.shape[0], im.shape[1]), cmap=plt.cm.Greys)
It would help in some cases.
Upvotes: 0
Reputation: 370
I guess you may have this problem in Pycharm. If so, you may try this to your problem.
Go to File-Setting-Tools-Python Scientific
in Pycharm and remove the option of Show plots in tool window
.
Upvotes: 0
Reputation: 71
Try to use this,
plt.imshow(numpy.real(A))
plt.show()
instead of plt.imshow(A)
Upvotes: 3
Reputation: 796
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
Upvotes: 63
Reputation: 1249
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.
Upvotes: 45
Reputation: 14075
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()
Upvotes: 3
Reputation: 1619
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().
Upvotes: 3