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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 5 Posters 7.0k Views 4 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.
  • 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
                                • SGaistS SGaist

                                  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.

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

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

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

                                  Yes, but the question has been, how do I make the original image lighter and reflect this when copied to the clipboard?

                                  raven-worxR 1 Reply Last reply
                                  0
                                  • btseB btse

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

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

                                    Yes, but the question has been, how do I make the original image lighter and reflect this when copied to the clipboard?

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

                                    @btse
                                    and what about the way i already suggested?!

                                        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
                                        buffer.close();
                                    
                                        QMimeData *mimeData = new QMimeData;
                                             mimeData->setData("image/png", bytes);
                                        QApplication::clipboard()->setMimeData(mimeData);
                                    

                                    This should definitely work, even when the your used clipboard inspector doesn't display it correctly.

                                    --- 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

                                    btseB 1 Reply Last reply
                                    0
                                    • raven-worxR raven-worx

                                      @btse
                                      and what about the way i already suggested?!

                                          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
                                          buffer.close();
                                      
                                          QMimeData *mimeData = new QMimeData;
                                               mimeData->setData("image/png", bytes);
                                          QApplication::clipboard()->setMimeData(mimeData);
                                      

                                      This should definitely work, even when the your used clipboard inspector doesn't display it correctly.

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

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

                                      @btse
                                      and what about the way i already suggested?!

                                          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
                                          buffer.close();
                                      
                                          QMimeData *mimeData = new QMimeData;
                                               mimeData->setData("image/png", bytes);
                                          QApplication::clipboard()->setMimeData(mimeData);
                                      

                                      This should definitely work, even when the your used clipboard inspector doesn't display it correctly.

                                      It simply doesn't work, not sure what else to tell you. I tried pasting it to many different applications, none of them recognize the clipboard's contents. Have you been able to get this example to work? I've been testing on Mac OS 10.11.6 with Qt 4.8.7.2.

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        Basilio
                                        wrote on last edited by
                                        #24

                                        Two years too late, but as I was having the exact same issue it might help a lost soul in the future.

                                        A workaround for this is to save a temporary image and copy to clipboard the url to the file. This is recognized in most software on Mac (PPTX, Keynote included) and should work on Windows.

                                        void ToClipboard(const QPixmap& pixmap)
                                        {
                                            QString filename = "/tmp/screenshot.png";
                                            pixmap.save(filename);
                                            QList<QUrl> urls;
                                            urls << QUrl(filename);
                                            QMimeData* outputMime = new QMimeData;
                                            outputMime->setUrls(urls);
                                            qApp->clipboard()->setMimeData(outputMime);
                                        }
                                        

                                        Take care of the typical caveats when writing to disk from an application, but for most applications that might need this (user-generated, user-controlled operations) this is an ok workaround.

                                        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