Skip to content

ImageGrab.grabclipboard() prints error instead of raising it on Linux X11 with text in the clipboard #7110

@Mar2ck

Description

@Mar2ck

What did you do?

Call ImageGrab.grabclipboard() on Linux running X11 while the clipboard contains text

What did you expect to happen?

Raise all errors (so exception handling can be done)

What actually happened?

First, UnidentifiedImageError is raised which is fine as it can be suppressed by using exception handling.
However a second error happens Error: target image/png not available but instead of being raised it's printed directly to the console, probably since it's what xclip is sending to stdout.

Because of this simply calling ImageGrab.grabclipboard() with text in the clipboard will always print to the console. I'm using Pillow to constantly check the clipboard for new images so this behaviour causes the console to be flooded with error messages.

What are your OS, Python and Pillow versions?

  • OS: KDE Neon 5.27 (Ubuntu Jammy)
  • Python: 3.10.6
  • Pillow: 9.5.0

The following code should print nothing when the clipboard is text but instead prints the error every loop:

import time
from PIL import Image, ImageGrab

print('Reading from clipboard')

img = None
while True:
    try:
        img = ImageGrab.grabclipboard()
    except:
        pass
    else:
        if isinstance(img, Image.Image):
            print("Successful Image Grab")

    time.sleep(1)

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