`QImage` constructor has unknown keyword `data`

后端 未结 2 1922
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 14:48

Suppose I am taking an image from the webcam using opencv.

_, img = self.cap.read()  # numpy.ndarray (480, 640, 3)

Then I create a QIma

相关标签:
2条回答
  • 2020-11-30 15:08

    What they are indicating is that the data is required as a parameter, not that the keyword is called data, the following method makes the conversion of a numpy/opencv image to QImage:

    from PyQt5.QtGui import QImage, qRgb
    import numpy as np
    import cv2
    
    gray_color_table = [qRgb(i, i, i) for i in range(256)]
    
    def NumpyToQImage(im):
        qim = QImage()
        if im is None:
            return qim
        if im.dtype == np.uint8:
            if len(im.shape) == 2:
                qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8)
                qim.setColorTable(gray_color_table)
            elif len(im.shape) == 3:
                if im.shape[2] == 3:
                    qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888)
                elif im.shape[2] == 4:
                    qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32)
        return qim
    
    img = cv2.imread('/path/of/image')
    qimg = NumpyToQImage(img)
    assert(not qimg.isNull())
    

    or you can use the qimage2ndarray library

    When using the indexes to crop the image is only modifying the shape but not the data, the solution is to make a copy

    img = cv2.imread('/path/of/image')
    img = np.copy(img[200:500, 300:500, :]) # copy image
    qimg = NumpyToQImage(img)
    assert(not qimg.isNull())
    
    0 讨论(0)
  • 2020-11-30 15:08

    I suspect it is erroring out with TypeError: 'data' is an unknown keyword argument because that is the first argument that it encounters.

    The linked class reference is for PyQt4, for PyQt5 it links to C++ documentation at https://doc.qt.io/qt-5/qimage.html, but the similarities are clear.

    PyQt4:

    QImage.__init__ (self, bytes data, int width, int height, int bytesPerLine, Format format)

    Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).

    PyQt5 (C++):

    QImage(const uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)

    Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).

    Per the examples at https://www.programcreek.com/python/example/106694/PyQt5.QtGui.QImage, you might try

    qimg = QImage(img, img.shape[1], img.shape[0], img.strides[0], QImage.Format_Indexed8)
    

    (without the data=, width=, etc)

    0 讨论(0)
提交回复
热议问题