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

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 这句话, 变量名其实无所谓的. 只要加这么一句话就可以了.

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

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值