remove little-endian assumption for image export

This commit is contained in:
KIU Shueng Chuan 2021-06-09 09:06:02 +08:00
parent 6f0ffcbf8f
commit 765f9648cd
2 changed files with 18 additions and 23 deletions

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)