gpt4 book ai didi

python - 将 QPixmap 转换为灰度图

转载 作者:太空宇宙 更新时间:2023-11-03 20:01:11 26 4
gpt4 key购买 nike

我正在尝试将 QPixmap 图像转换为灰度图像。但是,我尝试过的所有解决方案都出现错误。

第一次尝试:

def convertCv2ToQimage(self, cv2image):
height, width = cv2image.shape
return QtGui.QImage(cv2image.copy().data, width, height, width, QtGui.QImage.Format_Grayscale8)

result = pixmap.copy(r)
Q_image = result.toImage()
raw_img = cv2.imread(Q_image)
gray_img = cv2.cvtColor(raw_img, cv2.COLOR_BGR2GRAY)
final_result = self.convertCv2ToQimage(gray_img)
pixmap = QtGui.QPixmap.fromImage(final_result)
self.Changed_view.emit(pixmap)

错误:

TypeError: bad argument type for built-in operation.

第二次尝试:

result = pixmap.copy(r)
Q_image = QtGui.QPixmap.toImage(result)
qimage = QtGui.QImage(Q_image, Q_image.width, Q_image.height, QtGui.QImage.Format_Grayscale8)
pixmap = QtGui.QPixmap.fromImage(qimage)
self.Changed_view.emit(pixmap)

错误:

TypeError: arguments did not match any overloaded call:   QImage():
too many arguments QImage(QSize, QImage.Format): argument 1 has
unexpected type 'QImage' QImage(int, int, QImage.Format): argument 1
has unexpected type 'QImage' QImage(bytes, int, int, QImage.Format):
argument 1 has unexpected type 'QImage' QImage(sip.voidptr, int,
int, QImage.Format): argument 1 has unexpected type 'QImage'
QImage(bytes, int, int, int, QImage.Format): argument 1 has unexpected
type 'QImage' QImage(sip.voidptr, int, int, int, QImage.Format):
argument 1 has unexpected type 'QImage' QImage(List[str]): argument
1 has unexpected type 'QImage' QImage(str, format: str = None):
argument 1 has unexpected type 'QImage' QImage(QImage): too many
arguments QImage(Any): too many arguments

最佳答案

您可以使用convertToFormat()可以在 QImage 格式之间进行转换。

result = pixmap.copy(r)
Q_image = QtGui.QPixmap.toImage(result)
grayscale = image.convertToFormat(QtGui.QImage.Format_Grayscale8)
pixmap = QtGui.QPixmap.fromImage(grayscale)
self.Changed_view.emit(pixmap)

我不知道第一个错误(没有完整的回溯,很难看出问题的根源是什么),但第二个错误的发生是因为您在初始化时使用了错误的参数:如果您提供QImage 不能添加其他参数(如上次错误报告)。

关于python - 将 QPixmap 转换为灰度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59223743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com