微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

缩放Q

如何解决缩放Q

简短上下文:

我有一个QMainwindow,里面有一个mpv播放器。我使用mpv播放视频,使用PIL创建叠加图像,然后在pyqt窗口中全部运行。重叠图像会在视频的每个帧中或多或少地更新。

这是我的问题:

如果mpv图片很大,则更新覆盖图像太慢了(我已经进行了大量优化以提高性能,使用单独的进程和线程,仅使用一个覆盖等)。但是,如果图片很小,则所有图片都可以正常工作(因此表明效果不令人满意)。

我不介意为了获得性能而降低分辨率,所以我想拥有一个包含较低分辨率内容的大窗口。这可能吗?

这里的瓶颈是mpv的overlay.update函数

我的主要想法是缩放QMainwindow,但是我似乎找不到找到这种方法方法。任何其他解决方案当然都足够。

示例代码(请注意,test.mp4是硬编码的视频,请提供所有内容

#!/usr/bin/env python3
import mpv
import sys

from PIL import Image,ImageDraw
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Test(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.container = QWidget(self)
        self.setCentralWidget(self.container)
        self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)
        self.container.setAttribute(Qt.WA_NativeWindow)

        self.w = 1800
        self.h = int(self.w / 16 * 9)
        self.setFixedSize(self.w,self.h)
        
        self.player = mpv.MPV(wid=str(int(self.container.winId())),log_handler=print)
        self.player.play('test.mp4')


        self.overlay = self.player.create_image_overlay()
        self.coords = [20,20,50,50]

    def play(self):
        @self.player.property_observer("time-pos")
        def _time_observer(_name: str,value: float) -> None:
            
            for i in range(len(self.coords)):
                self.coords[i] = self.coords[i]*2 % self.h

            img = Image.new("RGBA",(self.w,self.h),(0,0))
            draw = ImageDraw.Draw(img)
            draw.rectangle(self.coords,outline=(255,255,255),width=4)
            
            self.overlay.update(img)
            
            

app = QApplication(sys.argv)

# This is necessary since PyQT stomps over the locale settings needed by libmpv.
# This needs to happen after importing PyQT before creating the first mpv.MPV instance.
import locale
locale.setlocale(locale.LC_NUMERIC,'C')
win = test()
win.show()
win.play()

sys.exit(app.exec_())

简短摘要

具有较大的窗口会导致mpv的overlay.update方法消耗过多的时间/计算量。降低重叠图片甚至视频的dpi(分辨率)以使其运行更快是可以接受的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。