Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Numpy array (gray scale) to QPixmap
Forum Updated to NodeBB v4.3 + New Features

Numpy array (gray scale) to QPixmap

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robopassio
    wrote on last edited by
    #1

    My input image is at grayscale, i.e., cv_image_array, which is a numpy array.
    I can successfully save it at as

    cv2.imwrite('gray.jpg', cv_image_array)
    

    or display it as

    cv2.imshow("Image", cv_image_array)
    cv2.waitKey(1)
    

    However, I want to display it directly on the UI

    I did:

    h, w = 320, 240
    q_img = QImage(cv_image_array, w, h, w, QImage.Format_Grayscale8)
    q_pixmap = QPixmap.fromImage(gray_img).scaled(h,w)
    _widget.image1.setPixmap(q_pixmap)
    

    However, the displayed image is broken and different from the one using cv2 commands
    I tried a lot of time but I cannot find a solution.
    Could someone please suggest some ideas to solve this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you sure the image is stored as a single channel and not RGB with only gray colors ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chong Yoe Yat
        wrote on last edited by
        #3

        Hi, i recently get into same problem also, what i did to solve is i changed the grayscale to color and replace frame.flatten() with frame.data and add bytesPerLine

        I write it down here just in case someone need this. : - )

        if len(cv_img.shape)<3:
            frame = cv2.cvtColor(cv_img, cv2.COLOR_GRAY2RGB)
        else:
            frame = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
        h, w = cv_img.shape[:2]
        bytesPerLine = 3 * w
        qimage = QImage(frame.data, w, h, bytesPerLine, QImage.Format.Format_RGB888) 
        #qimage = QImage(frame.flatten(), w, h, QImage.Format_RGB888)
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved