From 97e7bf8cb0c0e568e6ccae8c2f25bd1b25f88d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Eertmans?= Date: Fri, 2 Dec 2022 18:02:54 +0100 Subject: [PATCH] chore(speed): avoid unnecessary color conversion (#83) * chore(speed): avoid unnecessary color conversion This speeds up a bit the presentation by avoiding color conversion from BGR (OpenCV) to RGB. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- manim_slides/present.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/manim_slides/present.py b/manim_slides/present.py index 9c6a2fd..d6cf37c 100644 --- a/manim_slides/present.py +++ b/manim_slides/present.py @@ -11,7 +11,7 @@ import numpy as np from pydantic import ValidationError from PySide6 import QtGui from PySide6.QtCore import Qt, QThread, Signal, Slot -from PySide6.QtGui import QCloseEvent, QIcon, QKeyEvent, QPixmap, QResizeEvent +from PySide6.QtGui import QCloseEvent, QIcon, QImage, QKeyEvent, QPixmap, QResizeEvent from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QWidget from tqdm import tqdm @@ -598,24 +598,16 @@ class App(QWidget): # type: ignore @Slot(np.ndarray) def update_image(self, cv_img: np.ndarray) -> None: """Updates the (image) label with a new opencv image""" - self.pixmap = self.convert_cv_qt(cv_img) - self.label.setPixmap(self.pixmap) - - def convert_cv_qt(self, cv_img: np.ndarray) -> np.ndarray: - """Convert from an opencv image to QPixmap""" - rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) - h, w, ch = rgb_image.shape + h, w, ch = cv_img.shape bytes_per_line = ch * w - convert_to_Qt_format = QtGui.QImage( - rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888 - ) - p = convert_to_Qt_format.scaled( - self.width(), - self.height(), - self.aspect_ratio, - self.resize_mode, - ) - return QPixmap.fromImage(p) + qt_img = QImage(cv_img.data, w, h, bytes_per_line, QImage.Format_BGR888) + + if w != self.width() or h != self.height(): + qt_img = qt_img.scaled( + self.width(), self.height(), self.aspect_ratio, self.resize_mode + ) + + self.label.setPixmap(QPixmap.fromImage(qt_img)) @click.command()