Merge pull request #1568 from pijyoi/pyqt_qimage

Change image tests to not rely on non-const QImage
This commit is contained in:
Ogi Moore 2021-02-10 19:57:22 -08:00 committed by GitHub
commit 344348441a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -1358,21 +1358,23 @@ def imageToArray(img, copy=False, transpose=True):
The array will have shape (width, height, (b,g,r,a)). The array will have shape (width, height, (b,g,r,a)).
""" """
fmt = img.format() fmt = img.format()
ptr = img.bits() img_ptr = img.bits()
if QT_LIB in ['PySide', 'PySide2', 'PySide6']:
arr = np.frombuffer(ptr, dtype=np.ubyte)
else:
try:
# removed in Qt6
nbytes = img.byteCount()
except AttributeError:
# introduced in Qt 5.10
# however Python 3.7 + PyQt5-5.12 in the CI fails with
# "TypeError: QImage.sizeInBytes() is a private method"
nbytes = img.sizeInBytes()
ptr.setsize(nbytes)
arr = np.asarray(ptr)
if QT_LIB.startswith('PyQt'):
# sizeInBytes() was introduced in Qt 5.10
# however PyQt5 5.12 will fail with:
# "TypeError: QImage.sizeInBytes() is a private method"
# note that sizeInBytes() works fine with:
# PyQt5 5.15, PySide2 5.12, PySide2 5.15
try:
# 64-bits size
nbytes = img.sizeInBytes()
except (TypeError, AttributeError):
# 32-bits size
nbytes = img.byteCount()
img_ptr.setsize(nbytes)
arr = np.frombuffer(img_ptr, dtype=np.ubyte)
arr = arr.reshape(img.height(), img.width(), 4) arr = arr.reshape(img.height(), img.width(), 4)
if fmt == img.Format_RGB32: if fmt == img.Format_RGB32:
arr[...,3] = 255 arr[...,3] = 255

View File

@ -137,12 +137,14 @@ def assertImageApproved(image, standardFile, message=None, **kwargs):
QtGui.QApplication.processEvents() QtGui.QApplication.processEvents()
graphstate = scenegraphState(w, standardFile) graphstate = scenegraphState(w, standardFile)
image = np.zeros((w.height(), w.width(), 4), dtype=np.ubyte) qimg = QtGui.QImage(w.size(), QtGui.QImage.Format.Format_ARGB32)
qimg = fn.makeQImage(image, alpha=True, copy=False, transpose=False) qimg.fill(QtCore.Qt.GlobalColor.transparent)
painter = QtGui.QPainter(qimg) painter = QtGui.QPainter(qimg)
w.render(painter) w.render(painter)
painter.end() painter.end()
image = fn.imageToArray(qimg, copy=False, transpose=False)
# transpose BGRA to RGBA # transpose BGRA to RGBA
image = image[..., [2, 1, 0, 3]] image = image[..., [2, 1, 0, 3]]