Read an image with OpenCV and display it with Tkinter
You might want to take a look at this one. Here is something works for me:
import numpy as npimport cv2import Tkinter import Image, ImageTk# Load an color imageimg = cv2.imread('img.png')#Rearrang the color channelb,g,r = cv2.split(img)img = cv2.merge((r,g,b))# A root window for displaying objectsroot = Tkinter.Tk() # Convert the Image object into a TkPhoto objectim = Image.fromarray(img)imgtk = ImageTk.PhotoImage(image=im) # Put it in the display windowTkinter.Label(root, image=imgtk).pack() root.mainloop() # Start the GUI
For Python3 I had to modify @Ha Dang answer:
from tkinter import *from PIL import Image, ImageTkimport cv2import numpy as npimage_name = 'bla.jpg'image = cv2.imread(image_name)#Rearrang the color channelb,g,r = cv2.split(image)img = cv2.merge((r,g,b))# A root window for displaying objectsroot = Tk() # Convert the Image object into a TkPhoto objectim = Image.fromarray(img)imgtk = ImageTk.PhotoImage(image=im) # Put it in the display windowLabel(root, image=imgtk).pack() root.mainloop() # Start the GUI
Requirements were:
pip3
numpy==1.13.1opencv-python==3.3.0.9Pillow==4.2.1
brew
python3tcl-tk
For me both answers above did not work but where close. The following code did the trick for me (I also want to use place instead of pack):
image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) image = ImageTk.PhotoImage(image=Image.fromarray(image)) label_image = Label(self.detection, image=image) label_image.image = image label_image.place(x=0, y=0, anchor="w")