How do I output a colormap in a scene using pyqt?
<p><strong>TL;DR:</strong> You only need to read the "Update" section. </p>

<blockquote>
  <p>How do you output <code>numpy.random.random((256, 256))</code> as a colormap to a
  qt scene?</p>
</blockquote>

<p>That's the summary of the summary. </p>

<h2><strong>Update:</strong></h2>

<p>The following gets me the colormap I want saved to file. </p>

<pre><code>scaled_image = Image.fromarray(np.uint8(self.image*255))
plt.savefig("/home/test.png")
</code></pre>

<p><code>self.image</code> is 256x256 numpy array, all its values range between -1 and 1. 
<a href="https://i.stack.imgur.com/C6BtU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C6BtU.png" alt="enter image description here"></a></p>

<p>How do I get this image to output onto a scene in Qt? You can use <code>self.image = numpy.random.random((256, 256))</code> 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?</p>

<hr>

<h2>Update 24/02/1016</h2>

<p>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?</p>

<pre><code>    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)
</code></pre>

<p><a href="https://i.stack.imgur.com/CGxqD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CGxqD.png" alt="enter image description here"></a></p>

<h2>What I've tried 23/02/2016</h2>

<p>The following gves me a blank screen</p>

<pre><code>    scene = QGraphicsScene(self)
    scaled_image = Image.fromarray(np.uint8(self.image*255))
    pixMap = QPixmap(scaled_image)
    scene.addPixmap(pixMap)
    self.graphicsView.setScene(scene)
</code></pre>

<p><a href="https://i.stack.imgur.com/hyxq6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hyxq6.png" alt="enter image description here"></a></p>

<p>The following get me a grayscale outputted to the Qt scene. Why isn't it in colour?</p>

<pre><code>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)
</code></pre>

<p><a href="https://i.stack.imgur.com/9R3Jr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9R3Jr.png" alt="enter image description here"></a></p>

<p>I reverse engineered from solutions to colormap problems <a href="https://stackoverflow.com/questions/10965417/how-to-convert-numpy-array-to-pil-image-applying-matplotlib-colormap">here</a> and displaying an image <a href="https://stackoverflow.com/questions/8766584/displayin-an-image-in-a-qgraphicsscene">here</a>.</p>

<p>Using advice from a solution posted I tried creating a <code>QGraphicsPixmapItem</code> 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 <a href="https://srinikom.github.io/pyside-docs/PySide/QtGui/QGraphicsPixmapItem.html" rel="nofollow noreferrer">pyside docs</a>. Sadly I get the same grayscale as above. Code below:</p>

<pre><code>    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)
</code></pre>

<hr>

<h2>Other things I've tried (older):</h2>

<p><code>self.image</code> is numpy 2D array, all its values range between -1 and 1. </p>

<p>I scale as follows and get a grayscale image. However, I want a colourmap: </p>

<pre><code>    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
</code></pre>

<p><a href="https://i.stack.imgur.com/VCRDE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VCRDE.png" alt="enter image description here"></a></p>

<p>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</p>

<pre><code>&gt;&gt;&gt; fig = plt.figure()
&gt;&gt;&gt; ax = fig.add_subplot(111)
&gt;&gt;&gt; imgplot = ax.imshow(self.image)
</code></pre>

<p><a href="https://i.stack.imgur.com/QBW6N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QBW6N.png" alt="enter image description here"></a></p>

<p>How do I add a colormap to the qt scene, instead of just having a grayscale? </p>