当前位置 : 主页 > 编程语言 > python >

python tkinter 打开摄像头并解决闪烁问题

来源:互联网 收集:自由互联 发布时间:2022-06-15
from tkinter import * from PIL import Image , ImageTk from tkinter import ttk import os import platform import cv2 import threading capture = cv2 . VideoCapture ( 0 ) def loopCapture (): global capture global imgCanvas global win # imgpath


from tkinter import *
from PIL import Image,ImageTk
from tkinter import ttk
import os
import platform
import cv2
import threading

capture = cv2.VideoCapture(0)


def loopCapture():
global capture
global imgCanvas
global win

# imgpath = "./"
# image = "50618close.jpg"
abc=None
while(True):
_,frame= capture.read()
# cv2.imshow("org",frame)
cov= cv2.cvtColor(frame,cv2.COLOR_RGB2BGR) #初始图像是RGB格式,转换成BGR即可正常显示了

img =Image.fromarray(cov)
img =ImageTk.PhotoImage(img)
imgCanvas.create_image(0,0,anchor='nw',image=img)
abc = None
abc = img # 解决摄像头图像闪烁的问题..

def toggle_fullscreen(self, event=None):
win.attributes("-fullscreen", True)

def end_fullscreen(self, event=None):
win.attributes("-fullscreen", False)

def createUI():
global imgCanvas
global win
global frame1
global tree
global label_speed
win = Tk()
win.title("***系统")
# win.geometry("1920x1080")
sysstr = platform.system()

w = win.winfo_screenwidth()
h = win.winfo_screenheight()


win.geometry("%dx%d" %(w,h))
# win.attributes("-topmost",True)
# win.attributes('-fullscreen', True)
win.configure(bg="#fff")
win.bind("<F11>", toggle_fullscreen)
win.bind("<Escape>", end_fullscreen)


imgCanvas = Canvas(win,height=int(h * 0.8),width=w )
imgCanvas.pack(side='top', fill='x')


win.mainloop()


t=threading.Thread(target=loopCapture)
t.setDaemon(True)
t.start()

createUI()

解决摄像头图像闪烁的问题 主要起作用的就是 abc = img 这句话, 变量名其实无所谓的. 只要加这么一句话就可以了.

这句话的作用应该是起到了图片对象延迟销毁的作用.



网友评论