如何使用Python和Tkinter稳定打开摄像头避免画面闪烁?

2026-05-28 18:041阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计390个文字,预计阅读时间需要2分钟。

如何使用Python和Tkinter稳定打开摄像头避免画面闪烁?

从Tkinter导入所有,从PIL导入Image和ImageTk,导入os、platform、cv2和threading。创建视频捕获对象。定义循环捕获函数。


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

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

如何使用Python和Tkinter稳定打开摄像头避免画面闪烁?



本文共计390个文字,预计阅读时间需要2分钟。

如何使用Python和Tkinter稳定打开摄像头避免画面闪烁?

从Tkinter导入所有,从PIL导入Image和ImageTk,导入os、platform、cv2和threading。创建视频捕获对象。定义循环捕获函数。


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

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

如何使用Python和Tkinter稳定打开摄像头避免画面闪烁?