self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW)这样做会闪烁为什么
时间: 2024-03-29 12:36:12 浏览: 30
这段代码中的闪烁是由于Tkinter窗口的刷新机制引起的。当使用`canvas2.create_image()`方法在Tkinter画布上绘制图像时,画布会在每次刷新时重新绘制图像。在每次刷新时,画布会先清除之前的所有内容,然后重新绘制当前的所有图像。这个过程会导致画布上的图像在每次刷新时都会短暂消失,然后重新出现,从而产生闪烁的效果。
为了解决这个问题,我们可以将`ImageTk.PhotoImage`对象保存到一个全局变量或者对象属性中,以确保它的生命周期与Tkinter窗口的生命周期一致。这样,在每次刷新时,我们只需要更新已经存在的图像对象即可,而不需要每次重新创建一个新的`PhotoImage`对象。例如,在你提供的代码中,我们可以将`PhotoImage`对象保存到`self.photo`属性中,然后在每次刷新时,使用`self.photo`属性的值来更新画布上的图像。这样就可以避免闪烁的问题了。
相关问题
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW)这是是什么意思
这是一段使用OpenCV和Python的代码,用于将从摄像头获取的实时视频帧转换为图像,并将其显示在GUI界面上。具体来说,它执行以下操作:
1. 使用OpenCV库中的cvtColor()函数将从摄像头获取的BGR格式图像转换为RGB格式图像。
2. 使用Pillow库中的Image.fromarray()函数将NumPy数组转换为PIL图像对象。
3. 使用PIL库中的Image.resize()函数将图像缩放到指定的宽度和高度。
4. 使用PIL库中的ImageTk.PhotoImage()函数将PIL图像对象转换为Tkinter图像对象。
5. 使用Tkinter库中的Canvas.create_image()函数将图像显示在Canvas对象中。
其中,self.canvas2是一个Tkinter的Canvas对象,self.w和self.h是指定的宽度和高度,frame是从摄像头获取的实时视频帧。
class Camera(): def __init__(self, top): self.top = top self.h = int(data_demo.window_height // 1.5) self.w = int(data_demo.window_width // 1.74) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h, highlightthickness=2, highlightbackground='Black') self.canvas2.place(relx=0.0, rely=0.032) label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1) label.place(relx=0.32, rely=0.47, anchor='nw') def Thread_(self): self.bool = True self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display()) t.start() def windows_display(self): self.canvas2.delete('all') while self.bool: if data_demo.display_id is True: ref, frame = self.cap.read() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.canvas2.update() self.canvas2.after(15, self.windows_display())这个为什么会报错
这段代码中,`threading.Thread()` 方法的 `target` 参数应该传递一个函数名,而不是一个函数的返回值,因此需要将 `self.windows_display()` 修改为 `self.windows_display`。
此外,在 `windows_display()` 方法的最后,应该调用 `after()` 方法来实现定时更新画面,而不是在方法内部递归调用自身。递归调用会导致栈溢出,程序崩溃。
修改后的代码如下:
```python
class Camera():
def __init__(self, top):
self.top = top
self.h = int(data_demo.window_height // 1.5)
self.w = int(data_demo.window_width // 1.74)
self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h,
highlightthickness=2, highlightbackground='Black')
self.canvas2.place(relx=0.0, rely=0.032)
label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1)
label.place(relx=0.32, rely=0.47, anchor='nw')
def Thread_(self):
self.bool = True
self.cap = cv2.VideoCapture(0)
t = threading.Thread(target=self.windows_display)
t.start()
def windows_display(self):
self.canvas2.delete('all')
while self.bool:
if data_demo.display_id is True:
ref, frame = self.cap.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image1 = Image.fromarray(image)
image2 = image1.resize((self.w, self.h), Image.ANTIALIAS)
self.photo = ImageTk.PhotoImage(image=image2)
self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW)
self.canvas2.update()
self.canvas2.after(15)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)