Convert Python Opencv Image (numpy array) to PyQt QPixmap image

44,462

Solution 1

Use this to convert cvImage to Qimage, here cvImage is the original image.

height, width, channel = cvImg.shape
bytesPerLine = 3 * width
qImg = QImage(cvImg.data, width, height, bytesPerLine, QImage.Format_RGB888)

and set this Qimage to Label.setPixmap parameter from Qimage. It works!!!

Solution 2

Just complementing the answer of AdityaIntwala, if the image appears to be red or blue, it is because the format is not RGB, but BGR (the inverse). In this case, use the QImage.rgbSwapped method to correct:

height, width, channel = cvImg.shape
bytesPerLine = 3 * width
qImg = QImage(cvImg.data, width, height, bytesPerLine, QImage.Format_RGB888).rgbSwapped()

Solution 3

#image is the numpy array that you got from cv2.imread(example_image.jpg)

image = QtGui.QImage(image, image.shape[1],\
                            image.shape[0], image.shape[1] * 3,QtGui.QImage.Format_RGB888)
pix = QtGui.QPixmap(image)

self.scene.addPixmap(pix)

Solution 4

Hate to add to the large number of answers, but as this was the only thing that worked for me I will, in case others run into the same issue.

As mentioned here on GitHub

Wrap the numpy array/ndarry in a np.require(array, np.uint8, 'C') call first, such as:

arr2 = np.require(arr, np.uint8, 'C')
qImg = QtGui.QImage(arr2, width, height, QtGui.QImage.Format_RGB888)

Solution 5

There is the easiest way:

from PIL.ImageQt import ImageQt 
from PIL import Image
from PySide2.QtGui import QPixmap
import cv2


# Convert an opencv image to QPixmap
def convertCvImage2QtImage(cv_img):
    rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
    PIL_image = Image.fromarray(rgb_image).convert('RGB')
    return QPixmap.fromImage(ImageQt(PIL_image))
Share:
44,462

Related videos on Youtube

SimaGuanxing
Author by

SimaGuanxing

Updated on April 15, 2022

Comments

  • SimaGuanxing
    SimaGuanxing about 2 years

    I am trying to convert python opencv image to QPixmap.

    I follow the instruction shows Page Link and my code is attached below

    img = cv2.imread('test.png')[:,:,::1]/255. 
    imgDown = cv2.pyrDown(img)
    imgDown = np.float32(imgDown)        
    cvRGBImg = cv2.cvtColor(imgDown, cv2.cv.CV_BGR2RGB)
    qimg = QtGui.QImage(cvRGBImg.data,cvRGBImg.shape[1], cvRGBImg.shape[0], QtGui.QImage.Format_RGB888)
    pixmap01 = QtGui.QPixmap.fromImage(qimg)
    self.image01TopTxt = QtGui.QLabel('window',self)
    self.imageLable01 = QtGui.QLabel(self)
    self.imageLable01.setPixmap(pixmap01)
    

    The code has no compile and runtime error but the conversion is wrong and I just get some noise image. I am not sure what the problem is. Could anyone help?

  • jfs
    jfs almost 6 years
    I had to create QPixmap explicitly: self.imageLable01.setPixmap(QPixmap(qImg)) and pass bytesPerLine + 1
  • Overdrivr
    Overdrivr over 5 years
    Any documentation that mentions numpy format is always RGB888 ?
  • Mark Carpenter Jr
    Mark Carpenter Jr over 4 years
    Doesn't always have to be RGB888, from teh qt5 docs, doc.qt.io/qt-5/qimage.html#image-formats a list of supported formats. Of course, you'll have to make sure your numpy array has the right data.
  • Matthias
    Matthias almost 4 years
    @MarkCarpenterJr Is there any way to determine which image format I need? Tried several from your link but none works.
  • Mark Carpenter Jr
    Mark Carpenter Jr almost 4 years
    @Matthias You could try converting to a specific format first then creating the QImage(). You could also look at a tuple in the array, and guess at determining the format, RGB888, for example, is 8 bits per pixel and is considered a 24-bit format, a pixel would look like 3 8bit values; (255,255,255). All depends on what you're doing though.
  • fferri
    fferri about 2 years
    With OpenCV you want to use QImage.Format_BGR888 as OpenCV uses BGR, not RGB.
  • Luke_
    Luke_ about 2 years
    Hi and welcome to stack overflow, posting an answer with only a code block is not recommended, its better to have some text around it explaining how and why this works
  • s.paszko
    s.paszko almost 2 years
    @fferri When i try to use Format_BGR888 i have an error type object 'QImage' has no attribute 'Format_BGR888'. Otherwise with Format_RGB888 it works. Why?
  • fferri
    fferri almost 2 years
    @s.paszko QImage.Format_BGR888 was added in Qt 5.14, upgrade