summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTomi Valkeinen <tomi.valkeinen@ideasonboard.com>2022-05-30 17:27:10 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-06-01 12:08:54 +0300
commit733047476213010ad0831c3344430f51774578b8 (patch)
tree867634fb10001d884351a1826fbdbf2b4195aa10 /src
parent0971ea7c8b89e911cd25c1f756197b113c0020af (diff)
py: cam: Drop PIL dependency
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 <tomi.valkeinen@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src')
-rw-r--r--src/py/cam/cam_qt.py29
1 files 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 <tomi.valkeinen@ideasonboard.com>
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: