Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy to clipboard copies the image in .png format with large size (UPD: solution found, app with the fix inside comments) #3722

Open
Ariandr opened this issue Sep 14, 2024 · 7 comments · May be fixed by #3724
Labels
Unconfirmed Bug The bug is not confirmed by anyone else.

Comments

@Ariandr
Copy link

Ariandr commented Sep 14, 2024

Flameshot Version

12.1.0 and the last dev build

Installation Type

Using the ready-made package from Github Releases

Operating System type and version

macOS 14.6.1

Description

Hi.

Wherever I copy a screenshot to the clipboard it copies a huge file in .png format. Usually it's anywhere between 5 to 10 MB.
When I save image to a file, it saves .jpeg with 500-700 KB in size, so like 10-20 times smaller in size.
My Preferred save file extension setting is .jpeg.

Why doesn't copy to clipboard apply the same file type and compression as saving to the file?
Can I configure it somehow, so that I get the same result when copying to clipboard as saving to file?

I use copying to clipboard 95% of the time for work, but this file size constantly makes it problematic.
Thank you.

Steps to reproduce

  1. Run flameshot GUI
  2. Take a screenshot
  3. Click Copy selection to Clipboard or Command+C.
  4. Insert it anywhere where the app doesn't compress the image (e.g. Slack), the file is a .png that's large in file size

Screenshots or screen recordings

No response

System Information

image
I use the standard display of my Macbook.

@Ariandr Ariandr added the Unconfirmed Bug The bug is not confirmed by anyone else. label Sep 14, 2024
@Ariandr Ariandr changed the title Copy to clipboard copies the image in .tiff format with extremely large size Copy to clipboard copies the image in .png format with large size Sep 14, 2024
@Renari
Copy link

Renari commented Sep 14, 2024

image
Does this setting not work for you?

@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

Hi @Renari
I don't have this setting in GUI.
image

Is it possible to add it manually in the flameshot.ini file?
If so, can you give me a key?

Thanks

@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

This setting doesn't work for macOS.

When setting useJpgForClipboard=true in the flameshot.ini, it just doesn't copy anything to Clipboard and as a result I cannot paste it anywhere.

image

@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

Related to this issue:
#1840

@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

Made it work for macOS via osascript. Here is the code with a new function for macOS. It copies the compressed jpeg to the clipboard correctly.
The file is here:

void saveToClipboardMime(const QPixmap& capture, const QString& imageType)

#include <QTemporaryFile>
#include <QImageWriter>
#include <QPixmap>
#include <QByteArray>
#include <QProcess>
#include <QDebug>

void saveJpegToClipboardMacOS(const QPixmap& capture) {
    // Convert QPixmap to JPEG data
    QByteArray jpegData;
    QBuffer buffer(&jpegData);
    buffer.open(QIODevice::WriteOnly);

    QImageWriter imageWriter(&buffer, "jpeg");
    imageWriter.setQuality(ConfigHandler().jpegQuality()); // Set JPEG quality to whatever is in settings
    if (!imageWriter.write(capture.toImage())) {
        qWarning() << "Failed to write image to JPEG format.";
        return;
    }

    // Save JPEG data to a temporary file
    QTemporaryFile tempFile;
    if (!tempFile.open()) {
        qWarning() << "Failed to open temporary file for writing.";
        return;
    }
    tempFile.write(jpegData);
    tempFile.close();

    // Use osascript to copy the contents of the file to clipboard
    QProcess process;
    QString script = QString(
        "set the clipboard to (read (POSIX file \"%1\") as «class PNGf»)"
    ).arg(tempFile.fileName());
    process.start("osascript", QStringList() << "-e" << script);
    if (!process.waitForFinished()) {
        qWarning() << "Failed to execute AppleScript.";
    }

    // Clean up
    tempFile.remove();
}

void saveToClipboard(const QPixmap& capture)
{
    // If we are able to properly save the file, save the file and copy to
    // clipboard.
    if ((ConfigHandler().saveAfterCopy()) &&
        (!ConfigHandler().savePath().isEmpty())) {
        saveToFilesystem(capture,
                         ConfigHandler().savePath(),
                         QObject::tr("Capture saved to clipboard."));
    } else {
        AbstractLogger() << QObject::tr("Capture saved to clipboard.");
    }
    if (ConfigHandler().useJpgForClipboard()) {
#ifdef Q_OS_MAC
        saveJpegToClipboardMacOS(capture);
#else
        saveToClipboardMime(capture, "jpeg");
#endif
    } else {
        // Need to send message before copying to clipboard
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
        if (DesktopInfo().waylandDetected()) {
            saveToClipboardMime(capture, "png");
        } else {
            QApplication::clipboard()->setPixmap(capture);
        }
#else
        QApplication::clipboard()->setPixmap(capture);
#endif
    }
}

@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

Whoever needs the app with my fix for macOS, it's here:
flameshot.zip

I re-enabled the GUI setting to turn one the option to Use JPG for clipboard.

You can delete your previous app and then drag and drop the new one inside Applications, all your configuration settings will remain the same. When it requires to provide the screen capture permission, go to System Settings, delete the previous permission with - for old Flameshot, close the app, launch again and provide the permission.

Just for the context, before for the same screenshot it was a .png 7.6 MB in size, now it's a 850 KB JPEG with 0.9 compression quality.

@Ariandr Ariandr changed the title Copy to clipboard copies the image in .png format with large size Copy to clipboard copies the image in .png format with large size (solution found, updated app inside comments) Sep 15, 2024
@Ariandr
Copy link
Author

Ariandr commented Sep 15, 2024

Created a pull request with the fix described above: #3724

@Ariandr Ariandr changed the title Copy to clipboard copies the image in .png format with large size (solution found, updated app inside comments) Copy to clipboard copies the image in .png format with large size (UPD: solution found, app with the fix inside comments) Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unconfirmed Bug The bug is not confirmed by anyone else.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants