TL;DR: You only need to read the "Update" section.
How do you output
numpy.random.random((256, 256))
as a colormap to a qt scene?
That's the summary of the summary.
Update:
The following gets me the colormap I want saved to file.
scaled_image = Image.fromarray(np.uint8(self.image*255))
plt.savefig("/home/test.png")
self.image
is 256x256 numpy array, all its values range between -1 and 1.
How do I get this image to output onto a scene in Qt? You can use self.image = numpy.random.random((256, 256))
to have a starting point similar to me. How do you get your 2D numpy array of random values onto the pyqt scene as a colormap?
Update 24/02/1016
So close. This seems to work, but the scale has been inversed. Blue is now hot and red is cold. How do I switch this around so that it looks like the image above?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
gcf().canvas.draw() # IMPORTANT!
stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
l, b, w, h = gcf().bbox.bounds
qImage = QtGui.QImage(stringBuffer,
w,
h,
QtGui.QImage.Format_ARGB32)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
self.graphicsView.setScene(scene)
What I've tried 23/02/2016
The following gves me a blank screen
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
pixMap = QPixmap(scaled_image)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
The following get me a grayscale outputted to the Qt scene. Why isn't it in colour?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
imgQ = ImageQt(scaled_image)
pixMap = QtGui.QPixmap.fromImage(imgQ)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
I reverse engineered from solutions to colormap problems here and displaying an image here.
Using advice from a solution posted I tried creating a QGraphicsPixmapItem
first and then adding the item to the scene. The official docs were a bit confusing so I finally got what I needed from the much crispier pyside docs. Sadly I get the same grayscale as above. Code below:
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
imgQ = ImageQt(scaled_image)
pixMap = QtGui.QPixmap.fromImage(imgQ)
pixMapItem = QtGui.QGraphicsPixmapItem(pixMap)
scene.addItem(pixMapItem)
self.graphicsView.setScene(scene)
Other things I've tried (older):
self.image
is numpy 2D array, all its values range between -1 and 1.
I scale as follows and get a grayscale image. However, I want a colourmap:
scene = QGraphicsScene(self)
scaled_image = (self.image + 1) * (255 / 2)
scene.addPixmap(QPixmap.fromImage(qimage2ndarray.array2qimage(scaled_image)))
self.graphicsView.setScene(scene)
#min(self.image) = -0.64462
#max(self.image) = 1.0
If I had instead done the following I would have gotten the colormap I want such as in the image below of a web app I have previously developed
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> imgplot = ax.imshow(self.image)
How do I add a colormap to the qt scene, instead of just having a grayscale?