2

I'm trying to convert QPixmap image to grayscale. However, I got an error for all tried solution.

The first try:

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)

Error :

TypeError: bad argument type for built-in operation.

The second try:

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)

Error :

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
Legorooj
  • 2,646
  • 2
  • 15
  • 35
Noor
  • 61
  • 1
  • 6

2 Answers2

3

You can use convertToFormat() that can convert between QImage formats.

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)

I don't know about the first error (without the full Traceback it's hard to see what is the source of the problem), but the second one happens because you're using wrong parameters for the initialization: if you provide a QImage you can't add other parameters (as the last error reports).

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thanks a lot, it works now without any errors. I have edit the answer by changing the wrong variable name. Thank you so much. – Noor Dec 09 '19 at 15:13
  • The edit has been rejected, as it didn't add anything useful; also, I used the names you used in your question, so there was no point in changing them. Remember that it's a common convention (and an [*official* code style guide suggestion](https://www.python.org/dev/peps/pep-0008/) to use upper case names for classes only, and lower case for variable and function names. – musicamante Dec 09 '19 at 21:06
0
        image = cv2.imread('logo.jpg')
        grayFrame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        cv2.imwrite("ornek1.jpg", grayFrame)    
        img = QtGui.QImage(self.frame, self.width, self.height, QtGui.QImage.Format_RGB888)
        img = QtGui.QPixmap.fromImage(img)
        pixmap = QPixmap("ornek1.jpg")
        pixmap=pixmap.scaled(240, 240)

or

        result = QPixmap("ornek1.jpg")
        Q_image = QtGui.QPixmap.toImage(result)
        
        grayscale = Q_image.convertToFormat(QtGui.QImage.Format_Grayscale8)
        pixmap = QtGui.QPixmap.fromImage(grayscale)
        pixmap=pixmap.scaled(240, 240)
flavves
  • 11
  • 1