当前位置: 代码迷 >> 综合 >> QImage: argument 1 has unexpected type 'memoryview' arguments did not match any overloaded call
  详细解决方案

QImage: argument 1 has unexpected type 'memoryview' arguments did not match any overloaded call

热度:30   发布时间:2023-09-28 22:15:55.0

先上错误:

raceback (most recent call last):File "E:/CV/Wang/main.py", line 42, in <module>w = MyMain()File "E:/CV/Wang/main.py", line 32, in __init__qt_img = QImage(img.data, width, height, QImage.Format_RGB888)
TypeError: arguments did not match any overloaded call:QImage(): too many argumentsQImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(str, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(str, int, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'memoryview'QImage(List[str]): argument 1 has unexpected type 'memoryview'QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'QImage(QImage): argument 1 has unexpected type 'memoryview'QImage(Any): too many arguments

任务:

通过cv2读取图片,然后截取其中一部分,在Qlabel里面显示出来。


过程:

直接通过把cv2读取的图片显示出来,是没有问题的,代码如下:

img = cv2.imread('./runner.jpg')
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
qt_img = QImage(img.data, width, height, QImage.Format_RGB888)
new_img = qt_img.scaled(self.pic.width(), self.pic.height())
self.label.setPixmap(QPixmap.fromImage(new_img))

当我打算截取一部分的时候,出现了上述错误,截取代码如下:

img = cv2.imread('./runner.jpg')
img = img[300:, 300:, :]
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
qt_img = QImage(img.data, width, height, QImage.Format_RGB888)
new_img = qt_img.scaled(self.pic.width(), self.pic.height())
self.label.setPixmap(QPixmap.fromImage(new_img))

解答:https://stackoverflow.com/questions/55468135/what-is-the-difference-between-an-opencv-bgr-image-and-its-reverse-version-rgb-i

正确程序为:

img = cv2.imread('./runner.jpg')
img = img[300:, 300:, :]
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
qt_img = QImage(img.data.tobytes(), width, height, QImage.Format_RGB888)
new_img = qt_img.scaled(self.pic.width(), self.pic.height())
self.label.setPixmap(QPixmap.fromImage(new_img))

 

  相关解决方案