From patchwork Mon May 30 14:27:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16111 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 090CBC326F for ; Mon, 30 May 2022 14:27:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 308ED6564F; Mon, 30 May 2022 16:27:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653920867; bh=1AFq90Ktt2OqgK4q7nRfejvXrZdM2ryLizgojc4jWLY=; 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=xt/kD7iqCQW6ylsdh/jKZwl/z9X+h+n+xll1SB6rnpts+kFyyBHEj2DPc1MABp9xp q1bhSTwXxF2XZCXtAuxr+09QtnVrJzPe+nHarM4cOLaBuSZSLSJ24guIZPZtVycVmV nQzmnlZ21YWDmnZ0yhQVzo/cn5YkhGV4ASDJ4KFva/D1UUKf1+TvVoPkLkMQ/SFnmW x1kzX2uYLW+oh8k6X3wuHR7LYlKSJb7jjWHT/sq16gzhpreKiyE0wkZnDkOiYerDGw ST5y4vOwN0D449Exkk4rySvnOYpVXkJKAT4wIsVX5q+pp3PmX4uGIYFmUM+r9rBd0D LTqrllwKe3oTw== 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 48C066563B for ; Mon, 30 May 2022 16:27:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ju5CRlAW"; 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 B5343F95; Mon, 30 May 2022 16:27:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653920861; bh=1AFq90Ktt2OqgK4q7nRfejvXrZdM2ryLizgojc4jWLY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ju5CRlAWRbzfO6WCN47nynQ5U3IOR9NMlvxUMpqgak9wu2Vtrkxlyj0t0q7AWtiO4 fQ4IIGLc4pKqFME5LTfGKeyTG1ca7WQv8CF7YVok4xbEMcrJDBjx8zdTTHZABBO9lg CqaKHWr4+vVdPlwra1KRIlFEg8XK/nN7rOZZhgfQ= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Mon, 30 May 2022 17:27:10 +0300 Message-Id: <20220530142722.57618-5-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220530142722.57618-1-tomi.valkeinen@ideasonboard.com> References: <20220530142722.57618-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 04/16] 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 | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py index 61a77f45..b6412bdf 100644 --- a/src/py/cam/cam_qt.py +++ b/src/py/cam/cam_qt.py @@ -2,18 +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 @@ -136,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: