0

THIS link describes a way to set a QPixmap without using QImage. How can I reproduce this in python?

what I have is a buffer, which represents an image.

what I need is to call something like

label.setPixmap(QPixmap.loadFromData(buffer))

to display the image. But before that, a format header has to be inserted, according to the link for a 300x300 grayscale image the header is something like "P6 300 300 255". I have no idea how to do that.

Here is the whole script:

import numpy as np
import cv2
import sys
from PySide.QtGui import QApplication,QLabel,QPixmap


if QApplication.instance() is not None:
    a=QApplication.instance()
else: 
    a = QApplication(sys.argv)

lbl=QLabel()    
header = bytearray(b"P6 20 20 255")
cvImg=np.zeros((20,20),dtype=np.uint8)
cv2.circle(cvImg,(9,9),9,49,-1)
buff=cvImg.data
ppm=header+buff
pixmap=QPixmap()
lbl.setPixmap(pixmap.loadFromData(ppm,40,format="PPM"))


sys.exit(a.exec_())

It says:

TypeError: 'PySide.QtGui.QPixmap.loadFromData' called with wrong argument types:
  PySide.QtGui.QPixmap.loadFromData(bytearray, int, str)
Supported signatures:
  PySide.QtGui.QPixmap.loadFromData(PySide.QtCore.QByteArray, str = None, PySide.QtCore.Qt.ImageConversionFlags = Qt.AutoColor)
  PySide.QtGui.QPixmap.loadFromData(PySide.QtCore.uchar, unsigned int, str = None, PySide.QtCore.Qt.ImageConversionFlags = Qt.AutoColor)
Community
  • 1
  • 1
Anton Alice
  • 723
  • 1
  • 6
  • 10
  • You need to pass in `bytes` (or a `QByteArray`), and omit the length argument. Also, `loadFRomData` works in place, and returns a `bool`. But even with these changes, I doubt whether your example will work, because the format is probably not right. I don't know how to fix that, though. Why are you trying to do this? It doesn't seem like you will get much of a performance gain. – ekhumoro Oct 07 '16 at 19:20
  • I tried to do this, to find out how much performance gain can be achieved. – Anton Alice Oct 07 '16 at 19:38
  • You might want to try drawing a `QImage` directly with [QPainter](http://doc.qt.io/qt-5/qpainter.html). – ekhumoro Oct 07 '16 at 20:25
  • you mean directly drawing the circles, et cetera, instead of using numpy/opencv? no this was only an example. there is going to be a lot more done to the images than just drawing basic geometry on them. – Anton Alice Oct 07 '16 at 21:28
  • No, I mean [drawImage](http://doc.qt.io/qt-5/qpainter.html#drawImage) and friends. Of course I understand that you need to use opencv to create the images. – ekhumoro Oct 07 '16 at 21:46
  • I dont know how to use it(with my current script the kernel crashes immediately), but I guess a PaintDevice conversely has no setMouseTracker() method. – Anton Alice Oct 07 '16 at 22:17
  • This [answer](http://stackoverflow.com/a/28291210/984421) has a simple demo using `drawImage` with opencv. It could easily be adapted to use an event-filter which processes `QEvent.Paint` events. I tried it, and it seems to work okay. – ekhumoro Oct 07 '16 at 23:34
  • It crashes for grayscale images. Try to load a numpy.zeros((500,500),dtype=numpy.uint8), and set the QImage format to Format_Indexed8. kernel crashes – Anton Alice Oct 13 '16 at 19:42
  • Have you made sure you are keeping a separate reference to both the `QImage` and the underlying data? – ekhumoro Oct 13 '16 at 19:54
  • It should be correct. The only thing I changed in the original code was the initialisation of `self.cvImage`, replacing it with a 3-channel `zeros(500,500,3)`,and deleting all cv2 stuff. And this works fine. After changing the channel number to 1, and replacing the `Format_RGB888` with `Format_Indexed8`, the problem occurs. – Anton Alice Oct 13 '16 at 21:02
  • I wish there was someone else looking in who had more experience with opencv (and image processing in general). I know python/qt pretty well, but I just don't know enough about the image-related stuff to give in-depth advice about how to get it working properly with pyside. – ekhumoro Oct 13 '16 at 21:26
  • Ok I found the problem. Its simply the wrong format. Format_Indexed8 is obviously not grayscale. Format_Grayscale8 is grayscale, but not available with PySide. So currently the only option with PySide is to use Labels and SetPixmap, because the format mismatch is corrected there for some reason... – Anton Alice Oct 13 '16 at 21:48
  • Are you absolutely commited to using pyside? Format_Grayscale8 is available in pyqt5. – ekhumoro Oct 13 '16 at 22:42
  • no, but its a little annoying to install pyqt5 on a raspberry pi. – Anton Alice Oct 13 '16 at 23:43
  • @ekhumoro do I see it right, that the Grayscale format is not available in pyqt4? What a shame. Grayscale is the foundation – Anton Alice Oct 17 '16 at 16:51
  • See [QTBUG-8651](https://bugreports.qt.io/browse/QTBUG-8651). What's the issue with PyQt5 on raspberry pi? – ekhumoro Oct 17 '16 at 17:01

0 Answers0