Categories
Mastering Development

PyQt5 OpenCV QImage, QPixmap Pictures are output incorrectly [duplicate]

I am using opencv to open an image and putting qimage into qpixmap.

Since the Korean route is included, imread is performed in the following way.

def imread(filename, flags=cv2.IMREAD_COLOR, dtype=np.uint8): 
    try: 
        n = np.fromfile(filename, dtype) 
        img = cv2.imdecode(n, flags) 
        return img 
    except Exception as e: 
        print(e) 
        return None

enter image description here

The image you want to pixmap

Photos were captured using the Windows built-in capture function.

enter image description here

Pixmap image in the program

The picture is straight, but when I print it out in the program, it comes out weird.

qimage = QImage(img.data.tobytes(), img.shape[1],
                img.shape[0], QImage.Format_RGB888).rgbSwapped()

I think there is a problem with this code.

Currently, only the two photos I captured are having this problem.
There is no error in one picture, but it is tinggi and the other picture is printed in a different shape as above.

enter image description here

Properties of both photos. [Photo resolution
, pixel, Bit level]

def pixmap_image(self, img):
    qimage = QImage(img.data.tobytes(), img.shape[1],
                    img.shape[0], QImage.Format_RGB888).rgbSwapped()
    
    qpixmap = QPixmap.fromImage(qimage).scaled(self.img_preview_viewer.width(), 
                                               self.img_preview_viewer.height(),
                                               aspectRatioMode=Qt.KeepAspectRatio)

    self.img_preview_viewer.setPixmap(qpixmap)

I would like to know why some pictures come out correctly and some do not come out properly.

Is the code wrong?

I assume this is the problem with the odd-resolution picture. I hope I’m right.

Leave a Reply

Your email address will not be published. Required fields are marked *