Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add utility function to get raw bytes from QImages (compat.py) #355

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions qtpy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PYQT6,
PYSIDE2,
PYSIDE6,
QtBindingsNotFoundError,
)
from .QtWidgets import QFileDialog

Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions qtpy/tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Loading