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. How to control the quality/filesize of images copied to the clipboard?
QtWS25 Last Chance

How to control the quality/filesize of images copied to the clipboard?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 5 Posters 6.9k 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.
  • btseB Offline
    btseB Offline
    btse
    wrote on last edited by btse
    #1

    I am trying to control the quality/filesize of a QImage/QPixMap that is copied to the clipboard. Is there a simple way to do this?

    I have tried using the "quality" parameter of QPixMap::save() to save it to a QByteArray. This works, as I can see the QByteArray's size changes depending on the quality. However, if I then try to recreate a QImage/QPixMap from the QByteArray so I can use the QClipBoard::setImage() or QClipBoard::setPixMap() functions, the filesize of the image in the clipboard is always the largest size!

    I have also tried setting the QByteArray directly onto the clipboard using:

    QMimeData *mimeData = new QMimeData;
    mimeData->setData("image/*", bytearray);
    QApplication::clipboard()->setMimeData(mimeData);
    

    But this is not properly recognized by the clipboard.

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

      Hi and welcome to devnet,

      I might be wrong but I think you have to replace the * with the exact image format you are providing with your QMimeData object.

      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
      • btseB Offline
        btseB Offline
        btse
        wrote on last edited by
        #3

        I have tried using "image/png" but still nothing.

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

          Might be a silly question but are you sure your QByteArray contains png data ?

          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
          • btseB Offline
            btseB Offline
            btse
            wrote on last edited by
            #5

            Yup. I used the "png" format when calling QPixMap::save(). I also tested saving the QByteArray to a file, and it saves as a PNG.

            I can also see the size of the saved PNGs changes as a I tweak the quality parameter.

            1 Reply Last reply
            0
            • btseB Offline
              btseB Offline
              btse
              wrote on last edited by
              #6

              This works as expected:

              QMimeData *mimeData = new QMimeData;
              mimeData->setImageData(QImage::fromData(bytearray));
              QApplication::clipboard()->setMimeData(mimeData);
              

              But re-creating a QImage from the QByteArray that is in PNG format (with decreased quality) causes the image in the clipboard to once again be max filesize, not the decreased filesize.

              raven-worxR 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Isn't the image stored uncompressed in the clipboard ?

                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
                • btseB btse

                  This works as expected:

                  QMimeData *mimeData = new QMimeData;
                  mimeData->setImageData(QImage::fromData(bytearray));
                  QApplication::clipboard()->setMimeData(mimeData);
                  

                  But re-creating a QImage from the QByteArray that is in PNG format (with decreased quality) causes the image in the clipboard to once again be max filesize, not the decreased filesize.

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @btse said in How to control the quality/filesize of images copied to the clipboard?:

                  QMimeData *mimeData = new QMimeData;
                  mimeData->setImageData(QImage::fromData(bytearray));
                  QApplication::clipboard()->setMimeData(mimeData);

                  But re-creating a QImage from the QByteArray that is in PNG format (with decreased quality) causes the image in the clipboard to once again be max filesize, not the decreased filesize.

                  Then why not simply use the QMimeData::setData() method directly?

                  QMimeData *mimeData = new QMimeData;
                  mimeData->setData("image/png", pngByteArray);
                  QApplication::clipboard()->setMimeData(mimeData);
                  

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  3
                  • btseB Offline
                    btseB Offline
                    btse
                    wrote on last edited by
                    #9

                    @SGaist I don't know...The problem is that QImage and QPixMap have no concept of compression/quality. I believe internally the amount of bytes used is mainly dependent on the dimensions. That's why loading the compressed PNG QByteArray back into a QImage causes the size to go back to its max, uncompressed size.

                    @raven-worx I did do that. It doesn't work. Nothing is copied to the clipboard. This is my exact code:

                    QMimeData *mimeData = new QMimeData;
                    mimeData->setData("image/png", generateByteArray(copyDialog));
                    QApplication::clipboard()->setMimeData(mimeData);
                    

                    I have verified that the return value of generateByteArray() is a valid PNG format by saving it to a file.

                    kshegunovK 1 Reply Last reply
                    0
                    • btseB btse

                      @SGaist I don't know...The problem is that QImage and QPixMap have no concept of compression/quality. I believe internally the amount of bytes used is mainly dependent on the dimensions. That's why loading the compressed PNG QByteArray back into a QImage causes the size to go back to its max, uncompressed size.

                      @raven-worx I did do that. It doesn't work. Nothing is copied to the clipboard. This is my exact code:

                      QMimeData *mimeData = new QMimeData;
                      mimeData->setData("image/png", generateByteArray(copyDialog));
                      QApplication::clipboard()->setMimeData(mimeData);
                      

                      I have verified that the return value of generateByteArray() is a valid PNG format by saving it to a file.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10
                          // Make a smaller version of the image (snippet is from http://doc.qt.io/qt-5/qpixmap.html#save-1).
                          QPixmap pixmap;
                          QByteArray bytes;
                          QBuffer buffer(&bytes);
                          buffer.open(QIODevice::WriteOnly);
                          pixmap.save(&buffer, "PNG", 0); // This is where you'd play with the quality parameter
                      
                          // Then save that in the clipboard ...
                          QMimeData *mimeData = new QMimeData;
                          // ...
                      

                      There's no reason to think Qt should abide by the compression/quality of images when they are in memory; it'd use the most convenient and portable way of representing them. Only when it comes to loading/saving you're to pass how the compression should be done.

                      Read and abide by the Qt Code of Conduct

                      btseB 1 Reply Last reply
                      0
                      • kshegunovK kshegunov
                            // Make a smaller version of the image (snippet is from http://doc.qt.io/qt-5/qpixmap.html#save-1).
                            QPixmap pixmap;
                            QByteArray bytes;
                            QBuffer buffer(&bytes);
                            buffer.open(QIODevice::WriteOnly);
                            pixmap.save(&buffer, "PNG", 0); // This is where you'd play with the quality parameter
                        
                            // Then save that in the clipboard ...
                            QMimeData *mimeData = new QMimeData;
                            // ...
                        

                        There's no reason to think Qt should abide by the compression/quality of images when they are in memory; it'd use the most convenient and portable way of representing them. Only when it comes to loading/saving you're to pass how the compression should be done.

                        btseB Offline
                        btseB Offline
                        btse
                        wrote on last edited by
                        #11

                        @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                            // Make a smaller version of the image (snippet is from http://doc.qt.io/qt-5/qpixmap.html#save-1).
                            QPixmap pixmap;
                            QByteArray bytes;
                            QBuffer buffer(&bytes);
                            buffer.open(QIODevice::WriteOnly);
                            pixmap.save(&buffer, "PNG", 0); // This is where you'd play with the quality parameter
                        

                        That's what I did already to generate a lower quality image, but I can't get it into the clipboard properly with a QByteArray.

                        kshegunovK 1 Reply Last reply
                        0
                        • btseB btse

                          @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                              // Make a smaller version of the image (snippet is from http://doc.qt.io/qt-5/qpixmap.html#save-1).
                              QPixmap pixmap;
                              QByteArray bytes;
                              QBuffer buffer(&bytes);
                              buffer.open(QIODevice::WriteOnly);
                              pixmap.save(&buffer, "PNG", 0); // This is where you'd play with the quality parameter
                          

                          That's what I did already to generate a lower quality image, but I can't get it into the clipboard properly with a QByteArray.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by kshegunov
                          #12

                          Perhaps I misunderstood then. What I mean is to save that data into the clipboard with your own mime type. Otherwise I believe Qt does serialization/deserialization through a QVariant internally. So try with something like:

                          QMimeData *mimeData = new QMimeData;
                          mimeData->setData("image/x-btse-png", bytes);
                          QApplication::clipboard()->setMimeData(mimeData);
                          

                          PS:
                          If that doesn't work, substitute with application/octet-stream and try with it.

                          Read and abide by the Qt Code of Conduct

                          btseB 1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            Perhaps I misunderstood then. What I mean is to save that data into the clipboard with your own mime type. Otherwise I believe Qt does serialization/deserialization through a QVariant internally. So try with something like:

                            QMimeData *mimeData = new QMimeData;
                            mimeData->setData("image/x-btse-png", bytes);
                            QApplication::clipboard()->setMimeData(mimeData);
                            

                            PS:
                            If that doesn't work, substitute with application/octet-stream and try with it.

                            btseB Offline
                            btseB Offline
                            btse
                            wrote on last edited by btse
                            #13

                            @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                            Perhaps I misunderstood then. What I mean is to save that data into the clipboard with your own mime type. Otherwise I believe Qt does serialization/deserialization through a QVariant internally. So try with something like:

                            QMimeData *mimeData = new QMimeData;
                            mimeData->setData("image/x-btse-png", bytes);
                            QApplication::clipboard()->setMimeData(mimeData);
                            

                            PS:
                            If that doesn't work, substitute with application/octet-stream and try with it.

                            I tried both suggestions, neither do anything. FYI, this is on Mac OS. Here's a screenshot of the clipboard's contents: 0_1507244487883_Screen Shot 2017-10-05 at 7.01.13 PM.png

                            And here is a screenshot of the clipboard when it's working properly, generated with:

                            QApplication::clipboard()->setImage(QImage::fromData(generateByteArray()));
                            

                            0_1507245298227_Screen Shot 2017-10-05 at 7.14.21 PM.png

                            kshegunovK 1 Reply Last reply
                            0
                            • btseB btse

                              @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                              Perhaps I misunderstood then. What I mean is to save that data into the clipboard with your own mime type. Otherwise I believe Qt does serialization/deserialization through a QVariant internally. So try with something like:

                              QMimeData *mimeData = new QMimeData;
                              mimeData->setData("image/x-btse-png", bytes);
                              QApplication::clipboard()->setMimeData(mimeData);
                              

                              PS:
                              If that doesn't work, substitute with application/octet-stream and try with it.

                              I tried both suggestions, neither do anything. FYI, this is on Mac OS. Here's a screenshot of the clipboard's contents: 0_1507244487883_Screen Shot 2017-10-05 at 7.01.13 PM.png

                              And here is a screenshot of the clipboard when it's working properly, generated with:

                              QApplication::clipboard()->setImage(QImage::fromData(generateByteArray()));
                              

                              0_1507245298227_Screen Shot 2017-10-05 at 7.14.21 PM.png

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #14

                              Something is still unclear to me ... are you trying to reduce the data size in the clipboard, or in your application. And after setting the mime to application/octet-stream have you deserialized the data from the receiving end? And how this relates to TIFF images, which are generally quite larger than pngs?

                              Read and abide by the Qt Code of Conduct

                              btseB 1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                Something is still unclear to me ... are you trying to reduce the data size in the clipboard, or in your application. And after setting the mime to application/octet-stream have you deserialized the data from the receiving end? And how this relates to TIFF images, which are generally quite larger than pngs?

                                btseB Offline
                                btseB Offline
                                btse
                                wrote on last edited by
                                #15

                                @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                                Something is still unclear to me ... are you trying to reduce the data size in the clipboard, or in your application. And after setting the mime to application/octet-stream have you deserialized the data from the receiving end? And how this relates to TIFF images, which are generally quite larger than pngs?

                                I am trying to reduce the size of the image in the clipboard. How can I deserialize it? I'm trying to use the contents of the clipboard in other applications, not my own. Example: Copy my image to the clipboard and then paste into a Powerpoint slide. My goal is to reduce the file size of the pasted image.

                                The screenshot I showed of it working properly is what happens when I copy to the clipboard as a QImage (see the code snippet). I guess internally it's converting it to a TIFF image.

                                kshegunovK 1 Reply Last reply
                                0
                                • btseB btse

                                  @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                                  Something is still unclear to me ... are you trying to reduce the data size in the clipboard, or in your application. And after setting the mime to application/octet-stream have you deserialized the data from the receiving end? And how this relates to TIFF images, which are generally quite larger than pngs?

                                  I am trying to reduce the size of the image in the clipboard. How can I deserialize it? I'm trying to use the contents of the clipboard in other applications, not my own. Example: Copy my image to the clipboard and then paste into a Powerpoint slide. My goal is to reduce the file size of the pasted image.

                                  The screenshot I showed of it working properly is what happens when I copy to the clipboard as a QImage (see the code snippet). I guess internally it's converting it to a TIFF image.

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  I understand now. Well unfortunately nothing else comes to mind. Does it work if you do nothing about the image and just put it into the clipboard (the ordinary way)?

                                  Read and abide by the Qt Code of Conduct

                                  btseB 1 Reply Last reply
                                  0
                                  • kshegunovK kshegunov

                                    I understand now. Well unfortunately nothing else comes to mind. Does it work if you do nothing about the image and just put it into the clipboard (the ordinary way)?

                                    btseB Offline
                                    btseB Offline
                                    btse
                                    wrote on last edited by
                                    #17

                                    @kshegunov said in How to control the quality/filesize of images copied to the clipboard?:

                                    Does it work if you do nothing about the image and just put it into the clipboard (the ordinary way)?

                                    Yes that works perfectly fine. But my goal is to reduce the size of the image. A 1289 × 440 image copied to the clipboard has a size of 1.7MB when pasted, which is just way too big. It's making the size of our PowerPoints huge.

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

                                      Then you should also resize your image to be smaller, change the encoding etc.

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

                                      btseB 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        Then you should also resize your image to be smaller, change the encoding etc.

                                        btseB Offline
                                        btseB Offline
                                        btse
                                        wrote on last edited by
                                        #19

                                        @SGaist said in How to control the quality/filesize of images copied to the clipboard?:

                                        Then you should also resize your image to be smaller, change the encoding etc.

                                        But does quality == image resolution? I guess I could just lower the image resolution, but I'd prefer not to if there's another solution.

                                        By encoding do you mean PNG, JPG, etc? How can I control that at the QImage/QPixMap level? They have no concept of encoding until saved to a QIODevice.

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

                                          You can't, QImage is an uncompressed hardware independent representation of the image that allows direct access to pixel data while QPixmap is an off-screen image representation that you can be used as a paint device so it's hardware dependent.

                                          If you want small images in your PowerPoint document you have to start with the original image and make them lighter.

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

                                          btseB 1 Reply Last reply
                                          3

                                          • Login

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