Skip to content Skip to sidebar Skip to footer

What Is The Data Format Read By The Function Cv2.imread? Working With Tkinter And Python

Good day, I am quite new to Python programming and I was tasked to do my own GUI with image inside my GUI. I have been doing some good progress but i was stuck when I want to inser

Solution 1:

The format returned by cv2.cvtColor(...) is of type numpy.ndarray. You need to convert it to format recognized by tkinter by using Pillow module:

from tkinter import *
from PIL import Image, ImageTk
import cv2

root = Tk()

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# convert to image format recognized by tkinter
img = Image.fromarray(img)
tkimg = ImageTk.PhotoImage(image=img)

Label(root, image=tkimg).pack()

root.mainloop()

Post a Comment for "What Is The Data Format Read By The Function Cv2.imread? Working With Tkinter And Python"