diff --git a/qtpy/compat.py b/qtpy/compat.py index 4c6d428f..13fa63b3 100644 --- a/qtpy/compat.py +++ b/qtpy/compat.py @@ -12,6 +12,7 @@ PYQT6, PYSIDE2, PYSIDE6, + QtBindingsNotFoundError, ) from .QtWidgets import QFileDialog @@ -200,3 +201,14 @@ def isalive(obj): return shiboken.isValid(obj) return None + + +# ============================================================================= +def getimagebytes(qimage): + if PYQT5: + return qimage.bits().asstring(qimage.byteCount()) + if PYQT6: + return qimage.bits().asstring(qimage.sizeInBytes()) + if PYSIDE2 or PYSIDE6: + return qimage.bits().tobytes() + raise QtBindingsNotFoundError diff --git a/qtpy/tests/test_compat.py b/qtpy/tests/test_compat.py index 1e1fc28f..320ef93a 100644 --- a/qtpy/tests/test_compat.py +++ b/qtpy/tests/test_compat.py @@ -1,9 +1,12 @@ """Test the compat module.""" + import sys import pytest from qtpy import QtWidgets, compat +from qtpy.QtCore import QRectF, QSize, Qt +from qtpy.QtGui import QBrush, QImage, QPainter from qtpy.tests.utils import not_using_conda @@ -22,3 +25,10 @@ def test_isalive(qtbot): with qtbot.waitSignal(test_widget.destroyed): test_widget.deleteLater() assert compat.isalive(test_widget) is False + + +def test_getimagebytes(qtbot): + """Test compat.getimagebytes""" + image = QImage(QSize(100, 100), QImage.Format_RGB32) + _bytes = compat.getimagebytes(image) + assert len(_bytes) == 100 * 100 * 4