python - PIL.ImageTk.PhotoImage 的更快替代方案是什么?

标签 python tkinter python-imaging-library tkinter-canvas opencv-python

我正在尝试显示视频,并且使用示例/解决方案来解决如何在 Tkinter 上播放视频的问题似乎并不能提供流畅和快速的渲染。
例如:

frame = self.vid_cap.read()
frame = cv2.resize(frame, (696, 486))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

self.latest_img_ = Image.fromarray(frame)
self.latest_img_ = ImageTk.PhotoImage(image=self.latest_img_)

self.canvas.create_image(0, 0, anchor="nw", image=self.latest_img_)
上面的代码可以工作,但是视频太慢了,即使使用像 self.canvas.after(1, self.video_loop_) 这样的东西.经过一些基准测试后,结果是以下行:
self.latest_img_ = ImageTk.PhotoImage(image=self.latest_img_)
无论我在四核 mac 还是 12 核设备上运行代码,都需要 9ms ~ 100ms,而它上面的整个代码(从捕获到将 numpy 数组转换为图像)只需要 0ms 到 1ms。ImageTk.PhotoImage 的更快替代方案是什么? ?

最佳答案

您应该保留 canvas 的引用。对象并对其进行修改,而不是每次都创建新对象。为此,您可以首先通过传递 image=None 创建一个占位符。 .下面是一个最小样本:

import tkinter as tk
import cv2
from PIL import Image, ImageTk


class GUI(tk.Tk):
    def __init__(self):
        super().__init__()
        self.canvas = tk.Canvas(self)
        self.canvas.pack(fill="both", expand=1)
        self.streaming_image = self.canvas.create_image(0, 0, anchor="nw", image=None)

    def show_frame(self):
        _, frame = self.vid_cap.read()
        frame = cv2.resize(frame, (696, 486))
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame)
        self.latest_img = ImageTk.PhotoImage(image=img)
        self.canvas.itemconfig(self.streaming_image, image=self.latest_img)
        self.after(10, self.show_frame)

    def start_video(self, path=None):
        self.vid_cap = cv2.VideoCapture(path)
        self.show_frame()


root = GUI()
root.start_video("your_path_here")
root.mainloop()

关于python - PIL.ImageTk.PhotoImage 的更快替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65110791/

相关文章:

python - 将 PIL numpy 3d 数组转换为 2d 亮度值

图像和 pil 模块的 Python 循环

python - 如何使用 Matplotlib 在 python 中定义和绘制 10*10 数组?

python - 在 python 2.6 上为 PyLucene+JCC 构建 Windows

python - 为什么通过 Homebrew 安装的 Python 不包括 Tkinter

python - 基于串行数据动态更新 Tkinter 窗口

python - 使用 PIL 从文件夹加载所有图像

python - 两个字符串python之间的正则表达式文本

python - 哪些 Twitter 包装库支持 Python 3.x?

python - tkinter 自动完成小部件小写偏差