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

grabclipboard KeyError: 'RGBA' on Windows #7464

Closed
PhilipYip1988 opened this issue Oct 14, 2023 · 5 comments
Closed

grabclipboard KeyError: 'RGBA' on Windows #7464

PhilipYip1988 opened this issue Oct 14, 2023 · 5 comments

Comments

@PhilipYip1988
Copy link

PhilipYip1988 commented Oct 14, 2023

What did you do?

Use the Snipping Tool or Snip and Sketch on windows 10/11. These use a PNG format by default with an alpha channel.

This gives an image in clipboard that can be copied in programs like paint (which by default use the PNG format with an alpha channel).

What did you expect to happen?

Using:

from PIL import ImageGrab
ImageGrab.grabclipboard()

Should display the image.

What actually happened?

The error message displays:

KeyError: 'RGBA'

Then displays the image.

The image should display without an error message. Likely the grabclipboard function was designed for end of life versions of Windows which probably used RGB (JPEG) and does not handle RGBA (PNG) used in newer Windows Versions.

What are your OS, Python and Pillow versions?

  • OS: Windows 11 22H2
  • Python: 3.11.5
  • Pillow: 10.0.1
@radarhere
Copy link
Member

Could you provide the full traceback of the error?

@PhilipYip1988
Copy link
Author

PhilipYip1988 commented Oct 15, 2023

Here is the full traceback of the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\JpegImagePlugin.py:639](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/JpegImagePlugin.py:639), in _save(im, fp, filename)
    638 try:
--> 639     rawmode = RAWMODE[im.mode]
    640 except KeyError as e:

KeyError: 'RGBA'

The above exception was the direct cause of the following exception:

OSError                                   Traceback (most recent call last)
File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\Image.py:643](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/Image.py:643), in Image._repr_image(self, image_format, **kwargs)
    642 try:
--> 643     self.save(b, image_format, **kwargs)
    644 except Exception as e:

File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\Image.py:2413](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/Image.py:2413), in Image.save(self, fp, format, **params)
   2412 try:
-> 2413     save_handler(self, fp, filename)
   2414 except Exception:

File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\JpegImagePlugin.py:642](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/JpegImagePlugin.py:642), in _save(im, fp, filename)
    641     msg = f"cannot write mode {im.mode} as JPEG"
--> 642     raise OSError(msg) from e
    644 info = im.encoderinfo

OSError: cannot write mode RGBA as JPEG

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\IPython\core\formatters.py:344](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/IPython/core/formatters.py:344), in BaseFormatter.__call__(self, obj)
    342     method = get_real_method(obj, self.print_method)
    343     if method is not None:
--> 344         return method()
    345     return None
    346 else:

File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\Image.py:661](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/Image.py:661), in Image._repr_jpeg_(self)
    656 def _repr_jpeg_(self):
    657     """iPython display hook support for JPEG format.
    658 
    659     :returns: JPEG version of the image as bytes
    660     """
--> 661     return self._repr_image("JPEG")

File [c:\Users\Philip\Miniconda3\envs\vscode\Lib\site-packages\PIL\Image.py:646](file:///C:/Users/Philip/Miniconda3/envs/vscode/Lib/site-packages/PIL/Image.py:646), in Image._repr_image(self, image_format, **kwargs)
    644 except Exception as e:
    645     msg = f"Could not save to {image_format} for display"
--> 646     raise ValueError(msg) from e
    647 return b.getvalue()

ValueError: Could not save to JPEG for display

@radarhere
Copy link
Member

radarhere commented Oct 15, 2023

Ok, thanks. From that traceback, this error isn't actually because of grabclipboard. It's because you're using IPython. This is the same as #7259 (comment), and should be resolved by #7266.

The fix should be included in Pillow 10.1.0, which is due to be released... today. If you follow #7348, you should shortly see that the new version has been released. If you then upgrade your version of Pillow, your problem should be resolved.

@radarhere
Copy link
Member

Pillow 10.1.0 has now been released with the fix.

@PhilipYip1988
Copy link
Author

I updated to the new version and can confirm that this issue is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants