[libcamera-devel,v3,18/18] py: cam_qt: use libcamera.formats
diff mbox series

Message ID 20220518131329.66994-19-tomi.valkeinen@ideasonboard.com
State Accepted
Headers show
Series
  • Misc Python bindings patches
Related show

Commit Message

Tomi Valkeinen May 18, 2022, 1:13 p.m. UTC
Use the new libcamera.formats in cam_qt.py.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
---
 src/py/cam/cam_qt.py | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Laurent Pinchart May 18, 2022, 1:58 p.m. UTC | #1
Hi Tomi,

Thank you for the patch.

On Wed, May 18, 2022 at 04:13:29PM +0300, Tomi Valkeinen wrote:
> Use the new libcamera.formats in cam_qt.py.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> ---
>  src/py/cam/cam_qt.py | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py
> index 91be2a08..142bb35e 100644
> --- a/src/py/cam/cam_qt.py
> +++ b/src/py/cam/cam_qt.py
> @@ -8,6 +8,7 @@ from numpy.lib.stride_tricks import as_strided
>  from PIL import Image
>  from PIL.ImageQt import ImageQt
>  from PyQt5 import QtCore, QtGui, QtWidgets
> +import libcamera as libcam
>  import numpy as np
>  import sys
>  
> @@ -87,9 +88,7 @@ def to_rgb(fmt, size, data):
>      w = size.width
>      h = size.height
>  
> -    fmt = str(fmt)
> -
> -    if fmt == 'YUYV':
> +    if fmt == libcam.formats.YUYV:
>          # YUV422
>          yuyv = data.reshape((h, w // 2 * 4))
>  
> @@ -111,20 +110,22 @@ def to_rgb(fmt, size, data):
>          rgb[:, :, 2] -= 226.8183044444304
>          rgb = rgb.astype(np.uint8)
>  
> -    elif fmt == 'RGB888':
> +    elif fmt == libcam.formats.RGB888:
>          rgb = data.reshape((h, w, 3))
>          rgb[:, :, [0, 1, 2]] = rgb[:, :, [2, 1, 0]]
>  
> -    elif fmt == 'BGR888':
> +    elif fmt == libcam.formats.BGR888:
>          rgb = data.reshape((h, w, 3))
>  
> -    elif fmt in ['ARGB8888', 'XRGB8888']:
> +    elif fmt in [libcam.formats.ARGB8888, libcam.formats.XRGB8888]:
>          rgb = data.reshape((h, w, 4))
>          rgb = np.flip(rgb, axis=2)
>          # drop alpha component
>          rgb = np.delete(rgb, np.s_[0::4], axis=2)
>  
> -    elif fmt.startswith('S'):
> +    elif str(fmt).startswith('S'):
> +        # \todo implement PixelFormatInfo and use it here

PixelFormatInfo is not exposed through the libcamera public API, so I'd
drop this comment.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +        fmt = str(fmt)
>          bayer_pattern = fmt[1:5]
>          bitspp = int(fmt[5:])
>  
> @@ -296,7 +297,7 @@ class MainWindow(QtWidgets.QWidget):
>              h = cfg.size.height
>              pitch = cfg.stride
>  
> -            if str(cfg.pixel_format) == 'MJPEG':
> +            if cfg.pixel_format == libcam.formats.MJPEG:
>                  img = Image.open(BytesIO(mfb.planes[0]))
>                  qim = ImageQt(img).copy()
>                  pix = QtGui.QPixmap.fromImage(qim)
Kieran Bingham May 18, 2022, 2:18 p.m. UTC | #2
Quoting Tomi Valkeinen (2022-05-18 14:13:29)
> Use the new libcamera.formats in cam_qt.py.
> 

Aha, this answers my question in the previous patch.


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> ---
>  src/py/cam/cam_qt.py | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py
> index 91be2a08..142bb35e 100644
> --- a/src/py/cam/cam_qt.py
> +++ b/src/py/cam/cam_qt.py
> @@ -8,6 +8,7 @@ from numpy.lib.stride_tricks import as_strided
>  from PIL import Image
>  from PIL.ImageQt import ImageQt
>  from PyQt5 import QtCore, QtGui, QtWidgets
> +import libcamera as libcam
>  import numpy as np
>  import sys
>  
> @@ -87,9 +88,7 @@ def to_rgb(fmt, size, data):
>      w = size.width
>      h = size.height
>  
> -    fmt = str(fmt)
> -
> -    if fmt == 'YUYV':
> +    if fmt == libcam.formats.YUYV:
>          # YUV422
>          yuyv = data.reshape((h, w // 2 * 4))
>  
> @@ -111,20 +110,22 @@ def to_rgb(fmt, size, data):
>          rgb[:, :, 2] -= 226.8183044444304
>          rgb = rgb.astype(np.uint8)
>  
> -    elif fmt == 'RGB888':
> +    elif fmt == libcam.formats.RGB888:
>          rgb = data.reshape((h, w, 3))
>          rgb[:, :, [0, 1, 2]] = rgb[:, :, [2, 1, 0]]
>  
> -    elif fmt == 'BGR888':
> +    elif fmt == libcam.formats.BGR888:
>          rgb = data.reshape((h, w, 3))
>  
> -    elif fmt in ['ARGB8888', 'XRGB8888']:
> +    elif fmt in [libcam.formats.ARGB8888, libcam.formats.XRGB8888]:
>          rgb = data.reshape((h, w, 4))
>          rgb = np.flip(rgb, axis=2)
>          # drop alpha component
>          rgb = np.delete(rgb, np.s_[0::4], axis=2)
>  
> -    elif fmt.startswith('S'):
> +    elif str(fmt).startswith('S'):
> +        # \todo implement PixelFormatInfo and use it here
> +        fmt = str(fmt)
>          bayer_pattern = fmt[1:5]
>          bitspp = int(fmt[5:])
>  
> @@ -296,7 +297,7 @@ class MainWindow(QtWidgets.QWidget):
>              h = cfg.size.height
>              pitch = cfg.stride
>  
> -            if str(cfg.pixel_format) == 'MJPEG':
> +            if cfg.pixel_format == libcam.formats.MJPEG:
>                  img = Image.open(BytesIO(mfb.planes[0]))
>                  qim = ImageQt(img).copy()
>                  pix = QtGui.QPixmap.fromImage(qim)
> -- 
> 2.34.1
>

Patch
diff mbox series

diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py
index 91be2a08..142bb35e 100644
--- a/src/py/cam/cam_qt.py
+++ b/src/py/cam/cam_qt.py
@@ -8,6 +8,7 @@  from numpy.lib.stride_tricks import as_strided
 from PIL import Image
 from PIL.ImageQt import ImageQt
 from PyQt5 import QtCore, QtGui, QtWidgets
+import libcamera as libcam
 import numpy as np
 import sys
 
@@ -87,9 +88,7 @@  def to_rgb(fmt, size, data):
     w = size.width
     h = size.height
 
-    fmt = str(fmt)
-
-    if fmt == 'YUYV':
+    if fmt == libcam.formats.YUYV:
         # YUV422
         yuyv = data.reshape((h, w // 2 * 4))
 
@@ -111,20 +110,22 @@  def to_rgb(fmt, size, data):
         rgb[:, :, 2] -= 226.8183044444304
         rgb = rgb.astype(np.uint8)
 
-    elif fmt == 'RGB888':
+    elif fmt == libcam.formats.RGB888:
         rgb = data.reshape((h, w, 3))
         rgb[:, :, [0, 1, 2]] = rgb[:, :, [2, 1, 0]]
 
-    elif fmt == 'BGR888':
+    elif fmt == libcam.formats.BGR888:
         rgb = data.reshape((h, w, 3))
 
-    elif fmt in ['ARGB8888', 'XRGB8888']:
+    elif fmt in [libcam.formats.ARGB8888, libcam.formats.XRGB8888]:
         rgb = data.reshape((h, w, 4))
         rgb = np.flip(rgb, axis=2)
         # drop alpha component
         rgb = np.delete(rgb, np.s_[0::4], axis=2)
 
-    elif fmt.startswith('S'):
+    elif str(fmt).startswith('S'):
+        # \todo implement PixelFormatInfo and use it here
+        fmt = str(fmt)
         bayer_pattern = fmt[1:5]
         bitspp = int(fmt[5:])
 
@@ -296,7 +297,7 @@  class MainWindow(QtWidgets.QWidget):
             h = cfg.size.height
             pitch = cfg.stride
 
-            if str(cfg.pixel_format) == 'MJPEG':
+            if cfg.pixel_format == libcam.formats.MJPEG:
                 img = Image.open(BytesIO(mfb.planes[0]))
                 qim = ImageQt(img).copy()
                 pix = QtGui.QPixmap.fromImage(qim)