Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Desaturating an image?
Forum Updated to NodeBB v4.3 + New Features

Desaturating an image?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 379 Views
  • 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.
  • D Offline
    D Offline
    donquibeats
    wrote on last edited by
    #1

    Hello

    In my PyQt5 program, I have a colour (RGB) QImage and I'm being asked for the option to desaturate it, to create a black-and-white version of the image for printing.

    So my question is, where to start? A bunch of Googling hasn't revealed a clear answer. Is there an image processing library that already has this covered? Or do I need to start making my own function that steps through the pixel data?

    Thanks for any pointers

    jsulmJ 1 Reply Last reply
    0
    • D donquibeats

      Hello

      In my PyQt5 program, I have a colour (RGB) QImage and I'm being asked for the option to desaturate it, to create a black-and-white version of the image for printing.

      So my question is, where to start? A bunch of Googling hasn't revealed a clear answer. Is there an image processing library that already has this covered? Or do I need to start making my own function that steps through the pixel data?

      Thanks for any pointers

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @donquibeats See https://stackoverflow.com/questions/27949569/convert-a-qimage-to-grayscale

      QImage image = ...;
      image.convertToFormat(QImage::Format_Grayscale8);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • D Offline
        D Offline
        donquibeats
        wrote on last edited by
        #3

        Thanks very much, not sure how I didn't find that in my searching.

        In the meantime it turned out to be quite easy to code it myself, and I ended up knocking up a function which could vary the saturation, rather than just making it greyscale. Here's the code below in case anyone finds it useful.

        img is a QImage and v is a float. v=0 will give black and white, v=0.5 will halve the colour saturation. v=2 will double the saturation, but I haven't added any min() or max() in the example so that would need to be added if v>1.

            for x in range(0, img.width()):
                for y in range(0, img.height()):
                    c = img.pixel(x, y)
                    r1, g1, b1 = qRed(c), qGreen(c), qBlue(c)
                    grey = (r1 + g1 + b1) / 3
                    r2 = grey + (v * (r1 - grey))
                    g2 = grey + (v * (g1 - grey))
                    b2 = grey + (v * (b1 - grey))
                    img.setPixel(x, y, qRgb(int(r2), int(g2), int(b2)))
        
        1 Reply Last reply
        2

        • Login

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