Skip to content

PIL.ImageGrab.grabclipboard() is lossy on Mac #7214

@abey79

Description

@abey79

Issue

On Mac, PIL.ImageGrab.grabclipboard() is lossy because it uses JPG:

commands = [
            'set theFile to (open for access POSIX file "'
            + filepath
            + '" with write permission)',
            "try",
            "    write (the clipboard as JPEG picture) to theFile",
            "end try",
            "close access theFile",
        ]

This results in lossy acquisition, which can be highly undesirable.

One such cases is when dealing with screenshots. The noise induced by the JPG conversion on otherwise relatively flat image content eventually leads to much bigger PNG output (if such format is desired in the end).

This can be easily fixed with the following change:

commands = [
            'set theFile to (open for access POSIX file "' + filepath + '" with write permission)',
            "try",
            "    write (the clipboard as «class PNGf») to theFile",
            "end try",
            "close access theFile",
        ]

Happy to submit a PR if there is willingness to apply this change.

What are your OS, Python and Pillow versions?

  • OS: macOS Ventura
  • Python: 3.11
  • Pillow: 9.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions