diff options
Diffstat (limited to 'src/silx/gui/utils/image.py')
-rw-r--r-- | src/silx/gui/utils/image.py | 78 |
1 files changed, 45 insertions, 33 deletions
diff --git a/src/silx/gui/utils/image.py b/src/silx/gui/utils/image.py index 1757e3e..b9ab7c3 100644 --- a/src/silx/gui/utils/image.py +++ b/src/silx/gui/utils/image.py @@ -1,6 +1,6 @@ # /*########################################################################## # -# Copyright (c) 2017-2022 European Synchrotron Radiation Facility +# Copyright (c) 2017-2023 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -39,7 +39,7 @@ from numpy.lib.stride_tricks import as_strided as _as_strided from .. import qt -def convertArrayToQImage(array): +def convertArrayToQImage(array: numpy.ndarray) -> qt.QImage: """Convert an array-like image to a QImage. The created QImage is using a copy of the array data. @@ -50,73 +50,81 @@ def convertArrayToQImage(array): Channels are expected to be either RGB or RGBA. :type array: numpy.ndarray of uint8 :return: Corresponding Qt image with RGB888 or ARGB32 format. - :rtype: QImage """ - array = numpy.array(array, copy=False, order='C', dtype=numpy.uint8) + array = numpy.array(array, copy=False, order="C", dtype=numpy.uint8) if array.ndim != 3 or array.shape[2] not in (3, 4): - raise ValueError( - 'Image must be a 3D array with 3 or 4 channels per pixel') + raise ValueError("Image must be a 3D array with 3 or 4 channels per pixel") if array.shape[2] == 4: format_ = qt.QImage.Format_ARGB32 # RGBA -> ARGB + take care of endianness - if sys.byteorder == 'little': # RGBA -> BGRA + if sys.byteorder == "little": # RGBA -> BGRA array = array[:, :, (2, 1, 0, 3)] else: # big endian: RGBA -> ARGB array = array[:, :, (3, 0, 1, 2)] - array = numpy.array(array, order='C') # Make a contiguous array + array = numpy.array(array, order="C") # Make a contiguous array else: # array.shape[2] == 3 format_ = qt.QImage.Format_RGB888 height, width, depth = array.shape qimage = qt.QImage( - array.data, - width, - height, - array.strides[0], # bytesPerLine - format_) + array.data, width, height, array.strides[0], format_ # bytesPerLine + ) return qimage.copy() # Making a copy of the image and its data -def convertQImageToArray(image): +def convertQImageToArray(image: qt.QImage) -> numpy.ndarray: """Convert a QImage to a numpy array. - If QImage format is not Format_RGB888, Format_RGBA8888 or Format_ARGB32, - it is first converted to one of this format depending on - the presence of an alpha channel. + If QImage format is not one of: + + - Format_Grayscale8 + - Format_RGB888 + - Format_RGBA8888 + - Format_ARGB32, + + it is first converted to one of this format. The created numpy array is using a copy of the QImage data. :param QImage image: The QImage to convert. - :return: The image array of RGB or RGBA channels of shape - (height, width, channels (3 or 4)) - :rtype: numpy.ndarray of uint8 + :return: Image array of uint8 of shape: + + - (height, width) for grayscale images + - (height, width, channels (3 or 4)) for RGB and RGBA images """ - rgba8888 = getattr(qt.QImage, 'Format_RGBA8888', None) # Only in Qt5 + supportedFormats = ( + qt.QImage.Format_Grayscale8, + qt.QImage.Format_ARGB32, + qt.QImage.Format_RGB888, + qt.QImage.Format_RGBA8888, + ) # Convert to supported format if needed - if image.format() not in (qt.QImage.Format_ARGB32, - qt.QImage.Format_RGB888, - rgba8888): + if image.format() not in supportedFormats: if image.hasAlphaChannel(): - image = image.convertToFormat( - rgba8888 if rgba8888 is not None else qt.QImage.Format_ARGB32) + image = image.convertToFormat(qt.QImage.Format_RGBA8888) else: image = image.convertToFormat(qt.QImage.Format_RGB888) format_ = image.format() - channels = 3 if format_ == qt.QImage.Format_RGB888 else 4 + if format_ == qt.QImage.Format_Grayscale8: + channels = 1 + elif format_ == qt.QImage.Format_RGB888: + channels = 3 + else: + channels = 4 ptr = image.bits() - if qt.BINDING == 'PyQt5': + if qt.BINDING == "PyQt5": ptr.setsize(image.byteCount()) - elif qt.BINDING == 'PyQt6': + elif qt.BINDING == "PyQt6": ptr.setsize(image.sizeInBytes()) - elif qt.BINDING in ('PySide2', 'PySide6'): + elif qt.BINDING == "PySide6": ptr = ptr.tobytes() else: raise RuntimeError("Unsupported Qt binding: %s" % qt.BINDING) @@ -125,17 +133,21 @@ def convertQImageToArray(image): view = _as_strided( numpy.frombuffer(ptr, dtype=numpy.uint8), shape=(image.height(), image.width(), channels), - strides=(image.bytesPerLine(), channels, 1)) + strides=(image.bytesPerLine(), channels, 1), + ) if format_ == qt.QImage.Format_ARGB32: # Convert from ARGB to RGBA # Not a byte-ordered format: do care about endianness - if sys.byteorder == 'little': # BGRA -> RGBA + if sys.byteorder == "little": # BGRA -> RGBA view = view[:, :, (2, 1, 0, 3)] else: # big endian: ARGB -> RGBA view = view[:, :, (1, 2, 3, 0)] + if channels == 1: # Remove channel dimension + view = view[:, :, 0] + # Format_RGB888 and Format_RGBA8888 do not need reshuffling channels: # They are byte-ordered and already in the right order - return numpy.array(view, copy=True, order='C') + return numpy.array(view, copy=True, order="C") |