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: while len(childs) > 0:
ch = childs.pop(0) ch = childs.pop(0)
tree = self.getPaintItems(ch) tree = self.getPaintItems(ch)
if (ch.flags() & ch.ItemStacksBehindParent) or \ if (ch.flags() & ch.GraphicsItemFlag.ItemStacksBehindParent) or \
(ch.zValue() < 0 and (ch.flags() & ch.ItemNegativeZStacksBehindParent)): (ch.zValue() < 0 and (ch.flags() & ch.GraphicsItemFlag.ItemNegativeZStacksBehindParent)):
preItems.extend(tree) preItems.extend(tree)
else: else:
postItems.extend(tree) postItems.extend(tree)

View File

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

View File

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

View File

@ -1,6 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import numpy as np
import pyqtgraph as pg import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
from pyqtgraph.exporters import ImageExporter from pyqtgraph.exporters import ImageExporter
import pyqtgraph.functions as fn
app = pg.mkQApp() app = pg.mkQApp()
@ -11,3 +14,15 @@ def test_ImageExporter_filename_dialog():
p = pg.plot() p = pg.plot()
exp = ImageExporter(p.getPlotItem()) exp = ImageExporter(p.getPlotItem())
exp.export() 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."