From patchwork Fri May 27 14:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16091 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 7D236C326D for ; Fri, 27 May 2022 14:45:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 24A0F6563F; Fri, 27 May 2022 16:45:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653662734; bh=fOxQi3MV/7Kaw5ML4agaGNJFUHdB1JvWz5wxvrcHCjE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=CZPoipOVKsA9HQB8wQ2GPWkIp43CUrhOyJN4jp9TWKZ4yeIK66367N5MrFHmW1RED ZV86xqosPRAgykNlAoM5xZwDyXGUcEZGOYOP+lWN02JncB7gz0LFNJCtysNQ7SZy2T cWlSv1ZUMH2AK9SNsBEVOOYoLLLfJthl1jPDbsWw0dqTnbgOely8twaeRpYXXMxeNm uYF+D9I/cGkGPwvX9fkjA58IKsat51F/+2+R9QcL4did7gSDIU7PhhstS+rQ5zNPXb aA4/uBzhiwCJJY7FXBg9NNIHHktLO5shtBRsdYyLfCAC4w4TaZi69uEeNybLjQjG8l k4UkmKRBMJwgg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C9483633A3 for ; Fri, 27 May 2022 16:45:14 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OJHlm/EB"; dkim-atps=neutral Received: from deskari.lan (91-156-85-209.elisa-laajakaista.fi [91.156.85.209]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3FB7E4326; Fri, 27 May 2022 16:45:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653662714; bh=fOxQi3MV/7Kaw5ML4agaGNJFUHdB1JvWz5wxvrcHCjE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OJHlm/EBi+Zb1TZ/STmCJqJ+7LyonVjhNyCGqXhXS1pyD5SNBkcZsic+6rPIeLSzv +TvBdnvEhxY7FsM8z8q91JdowD0uIqT2X5dHQYnu6B/xcQMtZ6lfYJa9TTyt/tvvhh B/6x3eE+BcLFlqInsWQB4S1znh7iLcMCnRIv1FvQ= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Fri, 27 May 2022 17:44:37 +0300 Message-Id: <20220527144447.94891-21-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220527144447.94891-1-tomi.valkeinen@ideasonboard.com> References: <20220527144447.94891-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 20/30] py: cam: Drop PIL dependency X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" We can use Qt directly to accomplish the same as we do with PIL. A minor downside is that loading MJPEG frame with Qt produces a "Corrupt JPEG data" warning. The resulting picture looks fine, though. So add a message handler to ignore that warning. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/cam/cam_qt.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py index af4b0f86..b6412bdf 100644 --- a/src/py/cam/cam_qt.py +++ b/src/py/cam/cam_qt.py @@ -2,17 +2,32 @@ # Copyright (C) 2022, Tomi Valkeinen from helpers import mfb_to_rgb -from io import BytesIO -from PIL import Image -from PIL.ImageQt import ImageQt from PyQt5 import QtCore, QtGui, QtWidgets import libcamera as libcam import libcamera.utils import sys + +# Loading MJPEG to a QPixmap produces corrupt JPEG data warnings. Ignore these. +def qt_message_handler(msg_type, msg_log_context, msg_string): + if msg_string.startswith("Corrupt JPEG data"): + return + + # For some reason qInstallMessageHandler returns None, so we won't + # call the old handler + if old_msg_handler is not None: + old_msg_handler(msg_type, msg_log_context, msg_string) + else: + print(msg_string) + + +old_msg_handler = QtCore.qInstallMessageHandler(qt_message_handler) + + def rgb_to_pix(rgb): - img = Image.frombuffer('RGB', (rgb.shape[1], rgb.shape[0]), rgb) - qim = ImageQt(img).copy() + w = rgb.shape[1] + h = rgb.shape[0] + qim = QtGui.QImage(rgb, w, h, QtGui.QImage.Format.Format_RGB888) pix = QtGui.QPixmap.fromImage(qim) return pix @@ -135,9 +150,8 @@ class MainWindow(QtWidgets.QWidget): cfg = stream.configuration if cfg.pixel_format == libcam.formats.MJPEG: - img = Image.open(BytesIO(mfb.planes[0])) - qim = ImageQt(img).copy() - pix = QtGui.QPixmap.fromImage(qim) + pix = QtGui.QPixmap(cfg.size.width, cfg.size.height) + pix.loadFromData(mfb.planes[0]) else: rgb = mfb_to_rgb(mfb, cfg) if rgb is None: