I'm writing a window gui to display feed from my camera. However, I keep getting this error.
**stream
im_pil = Image.fromarray(im_rgb)
NameError: name 'Image' is not defined
I tried to import image but it didn't help. Does anyone knows what went wrong? Here's my code:
import tkinter as tk
import cv2
from PIL import Image
window = tk.Tk()
window.title('Video Stream')
main = tk.Frame(window, bg="white")
main.grid()
video = tk.Label(main)
video.grid()
window.bind('<Escape>', lambda event: window.destroy())
status, frame = 0, []
cap = cv2.VideoCapture(0)
def stream():
global status, frame
status, frame = cap.read()
# 如果有影像的話
if status:
# 將 OpenCV 色改格式 ( BGR ) 轉換成 RGB
im_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# 將 OpenCV 圖檔轉換成 PIL
im_pil = Image.fromarray(im_rgb)
# 轉換成 ImageTK
imgTK = ImageTk.PhotoImage(image=im_pil)
# 放入圖片
video.configure(image=imgTK)
# 防止圖片丟失,做二次確認
video.image = imgTK
# 10 豪秒 後執行 stream 函式,這裡是模擬 While 迴圈的部分
window.after(10, stream)
stream()
window.mainloop()
cap.release()
cv2.destroyAllWindows()
Change the problematic row as follows:
imgTK = ImageTk.PhotoImage(im_pil)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.