I have a simple viewer in PyQt4, and I'm having some strange behavior when converting from a numpy array (8-bit grayscale) to a QImage to display. It seems to me like something is going haywire when I try to construct a QImage from a transposed array, even though the memory order should have been updated (i.e. it's not just a view with different strides).
Below is a heavily cut-down version of the viewer:
import numpy as np
from PyQt4 import QtGui
class SimpleViewer(QtGui.QMainWindow):
def __init__(self, data, parent=None):
super(SimpleViewer, self).__init__(parent=parent)
hgt, wid = data.shape
dmin = data.min()
dmax = data.max()
bdata = ((data - dmin)/(dmax - dmin)*255).astype(np.uint8)
img = QtGui.QImage(bdata, wid, hgt, QtGui.QImage.Format_Indexed8)
self.scene = QtGui.QGraphicsScene(0, 0, wid, hgt)
self.px = self.scene.addPixmap(QtGui.QPixmap.fromImage(img))
self.view = QtGui.QGraphicsView(self.scene)
self.setCentralWidget(self.view)
if __name__ == "__main__":
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(['python'])
xx, yy = np.meshgrid(np.arange(-100,100), np.arange(350))
zz = xx * np.cos(yy/350.*6*np.pi)
viewer = SimpleViewer(zz)
viewer.show()
app.exec_()
Now, as-is this produces something like this, which is fine:
However, I need to view my real images with the first dimension as "x" and the second dimension as "y" so it needs to be transposed (plus a y-flip so the origin is lower-left, but that's outside the scope here).
If I change hgt, wid = data.shape to wid, hgt = data.shape and do nothing else (which is wrong), I get the following. It makes sense, because the QImage is just reading the memory.
However, if I also pass in bdata.T.copy() instead of bdata, I'm expecting that the array will be transposed and the memory re-ordered by the copy operation. But what I get is this:
It's so close but off by just a hair for some reason. Also, I noticed that if the dimensions just happen to be integer multiples of each other (e.g. 200x100), then it comes out okay. What is going on? Have I just missed something really really stupid?
I can't tell if this is a PyQt issue or a numpy issue ... the transposed array plots fine with matplotlib, but matplotlib handles the striding so that may not mean anything.
I get the same behavior in Python 2.7 and Python 3.5. The PyQt4 version is 4.11.4, using Qt version 4.8.7. Numpy version is 1.10.1.


