Merge pull request #1824 from pijyoi/less_endian

remove little-endian assumption for image export
This commit is contained in:
Ogi Moore 2021-06-09 15:02:48 -07:00 committed by GitHub
commit 7e8d34ecd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 25 deletions

View File

@ -128,8 +128,8 @@ class Exporter(object):
while len(childs) > 0:
ch = childs.pop(0)
tree = self.getPaintItems(ch)
if (ch.flags() & ch.ItemStacksBehindParent) or \
(ch.zValue() < 0 and (ch.flags() & ch.ItemNegativeZStacksBehindParent)):
if (ch.flags() & ch.GraphicsItemFlag.ItemStacksBehindParent) or \
(ch.zValue() < 0 and (ch.flags() & ch.GraphicsItemFlag.ItemNegativeZStacksBehindParent)):
preItems.extend(tree)
else:
postItems.extend(tree)

View File

@ -1,8 +1,9 @@
from .Exporter import Exporter
from ..parametertree import Parameter
from ..Qt import QtGui, QtCore, QtSvg, QT_LIB
from ..Qt import QtCore, QtGui, QtWidgets
from .. import functions as fn
import numpy as np
import sys
translate = QtCore.QCoreApplication.translate
__all__ = ['ImageExporter']
@ -73,15 +74,8 @@ class ImageExporter(Exporter):
targetRect = QtCore.QRect(0, 0, w, h)
sourceRect = self.getSourceRect()
bg = np.empty((h, w, 4), dtype=np.ubyte)
color = self.params['background']
bg[:,:,0] = color.blue()
bg[:,:,1] = color.green()
bg[:,:,2] = color.red()
bg[:,:,3] = color.alpha()
self.png = fn.makeQImage(bg, alpha=True, copy=False, transpose=False)
self.bg = bg
self.png = QtGui.QImage(w, h, QtGui.QImage.Format.Format_ARGB32)
self.png.fill(self.params['background'])
## set resolution of image:
origTargetRect = self.getTargetRect()
@ -104,13 +98,18 @@ class ImageExporter(Exporter):
painter.end()
if self.params['invertValue']:
mn = bg[...,:3].min(axis=2)
mx = bg[...,:3].max(axis=2)
bg = fn.qimage_to_ndarray(self.png)
if sys.byteorder == 'little':
cv = slice(0, 3)
else:
cv = slice(1, 4)
mn = bg[...,cv].min(axis=2)
mx = bg[...,cv].max(axis=2)
d = (255 - mx) - mn
bg[...,:3] += d[...,np.newaxis]
bg[...,cv] += d[...,np.newaxis]
if copy:
QtGui.QApplication.clipboard().setImage(self.png)
QtWidgets.QApplication.clipboard().setImage(self.png)
elif toBytes:
return self.png
else:

View File

@ -558,14 +558,10 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
# swap B,R channels for Qt
tmp = pixels[...,0].copy()
pixels[...,0] = pixels[...,2]
pixels[...,2] = tmp
pixels = pixels[::-1] # flip vertical
pixels = pixels[::-1].copy() # flip vertical
img = fn.makeQImage(pixels, transpose=False)
return img
qimg = fn.ndarray_to_qimage(pixels, QtGui.QImage.Format.Format_RGBA8888)
return qimg
def renderToArray(self, size, format=GL_BGRA, type=GL_UNSIGNED_BYTE, textureSize=1024, padding=256):
w,h = map(int, size)

View File

@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
from pyqtgraph.exporters import ImageExporter
import pyqtgraph.functions as fn
app = pg.mkQApp()
@ -11,3 +14,15 @@ def test_ImageExporter_filename_dialog():
p = pg.plot()
exp = ImageExporter(p.getPlotItem())
exp.export()
def test_ImageExporter_toBytes():
p = pg.plot()
p.hideAxis('bottom')
p.hideAxis('left')
exp = ImageExporter(p.getPlotItem())
qimg = exp.export(toBytes=True)
qimg = qimg.convertToFormat(QtGui.QImage.Format.Format_RGBA8888)
data = fn.qimage_to_ndarray(qimg)
black = (0, 0, 0, 255)
assert np.all(data == black), "Exported image should be entirely black."