opencv tkinter image

Solutions on MaxInterview for opencv tkinter image by the best coders in the world

showing results for - "opencv tkinter image"
Lisa
15 Nov 2020
1import tkinter
2import cv2
3import PIL.Image, PIL.ImageTk
4
5# Create a window
6window = tkinter.Tk()
7window.title("OpenCV and Tkinter")
8
9# Load an image using OpenCV
10cv_img = cv2.cvtColor(cv2.imread("background.jpg"), cv2.COLOR_BGR2RGB)
11
12# Get the image dimensions (OpenCV stores image data as NumPy ndarray)
13height, width, no_channels = cv_img.shape
14
15# Create a canvas that can fit the above image
16canvas = tkinter.Canvas(window, width = width, height = height)
17canvas.pack()
18
19# Use PIL (Pillow) to convert the NumPy ndarray to a PhotoImage
20photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img))
21
22# Add a PhotoImage to the Canvas
23canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
24
25# Run the window loop
26window.mainloop()