Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

ImageGrab.grabclipboard raises ValueError: cannot read this XPM file

Open vsavkin2018 opened this issue 8 months ago • 4 comments

PIL cannot parse XPM from clipboard, while other options are available.

What did you do?

  1. Sent a test image (Lenna.png) to myself in Telegram.
  2. Selected "Copy Image" in Telegram desktop version.
  3. Run this test script:
from PIL import ImageGrab
ImageGrab.grabclipboard()

What did you expect to happen?

No output (success).

What actually happened?

Traceback (most recent call last):
  File "/home/vsavkin/Projects/screen-ocr/test.py", line 2, in <module>
    ImageGrab.grabclipboard()
  File "/home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL/ImageGrab.py", line 181, in grabclipboard
    im = Image.open(data)
         ^^^^^^^^^^^^^^^^
  File "/home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL/Image.py", line 3516, in open
    im = _open_core(
         ^^^^^^^^^^^
  File "/home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL/Image.py", line 3499, in _open_core
    im = factory(fp, filename)
         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL/ImageFile.py", line 144, in __init__
    self._open()
  File "/home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL/XpmImagePlugin.py", line 61, in _open
    raise ValueError(msg)
ValueError: cannot read this XPM file

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 24.04 LTS (Wayland)
  • Python: 3.12.3
  • Pillow: 11.1.0
--------------------------------------------------------------------
Pillow 11.1.0
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0]
--------------------------------------------------------------------
Python executable is /home/vsavkin/Projects/screen-ocr/env/bin/python3
Environment Python files loaded from /home/vsavkin/Projects/screen-ocr/env
System Python files loaded from /usr
--------------------------------------------------------------------
Python Pillow modules loaded from /home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL
Binary Pillow modules loaded from /home/vsavkin/Projects/screen-ocr/env/lib/python3.12/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.1.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.5.0
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3, compiled for zlib-ng 2.2.2
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.13, harfbuzz 10.1.0
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------

So, there are multiple images in clipboard:

$ wl-paste -l
image/xpm
image/xbm
image/webp
image/wbmp
image/tiff
image/tif
image/qoi
image/ppm
image/pgm
image/pbm
image/jxl
image/jpg
image/jfif
image/ico
image/icns
image/cur
image/bmp
image/avif
image/png
application/x-td-use-jpeg
image/jpeg
application/x-qt-image
$ wl-paste -t image | file -
/dev/stdin: X pixmap image text, 512 x 512 x 114002, 3 chars/pixel
$ wl-paste -t image/png | file -
/dev/stdin: PNG image data, 512 x 512, 8-bit/color RGB, non-interlaced

I propose to check MIME types and choose a better supported one (like image/png) when available.

vsavkin2018 avatar Apr 07 '25 12:04 vsavkin2018

How would you feel if we just improved XPM support?

Would you mind copying hopper.png, and then using this code to save the file,

import subprocess
args = ["wl-paste", "-t", "image"]
p = subprocess.run(args, capture_output=True)
with open("out", "rb") as fp:
    fp.write(p.stdout)

and then uploading it here? That would presumably give us a similar XPM file to examine, and a file that we could potentially include in our test suite to be distributed with Pillow.

radarhere avatar Apr 07 '25 12:04 radarhere

out.txt

Sounds great! Here is the file (I changed "rb" to "wb" in your code).

vsavkin2018 avatar Apr 07 '25 12:04 vsavkin2018

I've created #8874 to resolve this. I've included your image, which I've named hopper_rgb.xpm. If the PR is merged, the test image will be distributed under our license.

radarhere avatar Apr 09 '25 10:04 radarhere