0

I'm converting an RGB image to HSV, and trying to display the same in a Label. But I'm getting error.

My code snippet is:

 def hsv_img():
        img1=cv2.medianBlur(img,3)
        imghsv = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV)
        lw_range=np.array([160,170,50])
        up_range=np.array([179,250,220])
        imgthresh1=cv2.inRange(imghsv,lw_range,up_range)
        imgthresh=Image.open(imgthresh)
        re_hsv=imhsv.resize((360,360),Image.ANTIALIAS)
        imhsv1=ImageTk.PhotoImage(re_hsv)
        lb_hsv = Label(windo, image = imhsv1,relief=SOLID)
        lb_hsv.image=imhsv1
        lb_hsv.pack()
        lb_hsv.place(x=230,y=180) 

And my error is:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Balu\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "E:\python\track\guinew.py", line 215, in hsv_img
imhsv=Image.open(imgthresh)
File "C:\Users\Balu\AppData\Local\Enthought\Canopy32\System\lib\site-packages\PIL\Image.py", line 1956, in open
prefix = fp.read(16)

AttributeError: 'numpy.ndarray' object has no attribute 'read'

So how to display the HSV image, is there any other way then what i have tried? Any suggestions are welcome!

Thanks in advance!

user3483746
  • 125
  • 1
  • 4
  • 13

1 Answers1

1

The error is thrown when you call Image.open(imgthresh), because Image.open expects a file-like object, but imgthresh is a Numpy array.

Try removing that line altogether.

EDIT: Here's a complete version that works (on my machine):

from PIL import Image, ImageTk
from Tkinter import Tk, Label, SOLID
import cv2
import numpy as np

img = np.array(Image.open('some-file.png'))
window = Tk()

def hsv_img():
  img1=cv2.medianBlur(img,3)
  imghsv = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV)
  lw_range=np.array([160,170,50])
  up_range=np.array([179,250,220])
  imgthresh1=cv2.inRange(imghsv,lw_range,up_range)
  re_hsv=Image.fromarray(imghsv).resize((360,360),Image.ANTIALIAS)
  imhsv1=ImageTk.PhotoImage(re_hsv)
  lb_hsv = Label(window, image = imhsv1,relief=SOLID)
  lb_hsv.image=imhsv1
  lb_hsv.pack()
  lb_hsv.place(x=230,y=180)

hsv_img()
window.mainloop()

I had to rename some things, as well as add a call to Image.fromarray when you resize to 360x360.

It seems like most of the confusion stems from different numpy/PIL/OpenCV/Tkinter image formats. You might find this conversion guide useful, though it's slightly out of date.

perimosocordiae
  • 15,441
  • 13
  • 54
  • 71
  • 2
    Indeed, from the code snippet shown by the OP and as stated by @peri , it seems that there is no use for this line. However, I might add that if you want to **convert a Numpy array to a PIL image**, use the [Image.fromarray()](http://stackoverflow.com/questions/10965417/how-to-convert-numpy-array-to-pil-image-applying-matplotlib-colormap) function of the PIL library. – Raoul Apr 22 '14 at 23:13
  • @perimosocordiae If I remove the line as you specified, then how am I suppose to display the HSV image on the screen. I didn't get this. Can you help me with your own example, where you are using RGB image and convert it to HSV and displaying it. It will help me alot. – user3483746 Apr 23 '14 at 05:09