Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to run simple PIL python example, can't convert jpeg to float

so I wrote a simple python script

from PIL import Image
from pylab import *
im = array(Image.open('sample.jpg'))
imshow(im)

and i get this error from IDLE

Traceback (most recent call last):
  File "/home/michael/Dropbox/OpenCV/greyscale.py", line 5, in <module>
    imshow(im)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2722, in     imshow
   imlim=imlim, resample=resample, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes.py", line 7091, in imshow
im.set_data(X)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 418, in     set_data
    raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float

basically says i can not covert my image to float, anyone know what im missing

like image 736
shosh Avatar asked Jan 27 '13 00:01

shosh


2 Answers

I know this is coming very later but I thought I should answer just in case anyone else had the same problem. I ran into the same problem and the issue was the image I loaded did not exist in the project folder.so you need to check that sample.jpg exist and that it loads properly before using it.

if os.path.isfile('sample.jpg'): im = array(Image.open('sample.jpg'))
if im == None or im.size == 0: 
   print 'Image loaded is empty'
   sys.exit(1)
like image 189
Stephen Avatar answered Sep 27 '22 23:09

Stephen


What you have is close. Looking at the imshow docs, you need to pass either an Image OR a data array.

This should work:

from PIL import Image
from pylab import *
im = Image.open('sample.jpg')
imshow(im)


Here's what imshow() is expecting:

"X may be a float array, a uint8 array or a PIL image."

There are some more details here:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow

like image 34
Eric Olson Avatar answered Sep 27 '22 22:09

Eric Olson