Repository navigation
Problem with saving image from clipboard #1293
Description
Activity
UPD It's version problem, when I installed Pillow version 2.6.0 everything worked ok
Reproducible with Pilllow 2.8.0, 2.8.1, 2.8.2.
Not reproducible with Pillow 2.6.0, 2.7.0.E:\stufftodelete>cat test.py from PIL import ImageGrab ImageGrab.grabclipboard() E:\stufftodelete>pip freeze | grep illow Pillow==2.8.2 E:\stufftodelete>python test.py Traceback (most recent call last): File "test.py", line 2, in <module> ImageGrab.grabclipboard() File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 51, in grabclipboard return BmpImagePlugin.DibImageFile(io.BytesIO(data)) File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 97, in __init__ self._open() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 206, in _open self._bitmap() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 147, in _bitmap raise IOError("Unsupported BMP bitfields layout") IOError: Unsupported BMP bitfields layout E:\stufftodelete>pip install pillow==2.8.1 Collecting pillow==2.8.1 Using cached Pillow-2.8.1-cp27-none-win_amd64.whl Installing collected packages: pillow Found existing installation: Pillow 2.8.2 Uninstalling Pillow-2.8.2: Successfully uninstalled Pillow-2.8.2 Successfully installed pillow-2.8.1 E:\stufftodelete>python test.py Traceback (most recent call last): File "test.py", line 2, in <module> ImageGrab.grabclipboard() File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 51, in grabclipboard return BmpImagePlugin.DibImageFile(io.BytesIO(data)) File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 97, in __init__ self._open() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 206, in _open self._bitmap() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 147, in _bitmap raise IOError("Unsupported BMP bitfields layout") IOError: Unsupported BMP bitfields layout E:\stufftodelete>pip install pillow==2.8.0 Collecting pillow==2.8.0 Downloading Pillow-2.8.0-cp27-none-win_amd64.whl (1.3MB) 100% |################################| 1.3MB 386kB/s Installing collected packages: pillow Found existing installation: Pillow 2.8.1 Uninstalling Pillow-2.8.1: Successfully uninstalled Pillow-2.8.1 Successfully installed pillow-2.8.0 E:\stufftodelete>python test.py Traceback (most recent call last): File "test.py", line 2, in <module> ImageGrab.grabclipboard() File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 51, in grabclipboard return BmpImagePlugin.DibImageFile(io.BytesIO(data)) File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 97, in __init__ self._open() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 206, in _open self._bitmap() File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 147, in _bitmap raise IOError("Unsupported BMP bitfields layout") IOError: Unsupported BMP bitfields layout E:\stufftodelete>pip install pillow==2.7.0 Collecting pillow==2.7.0 Downloading Pillow-2.7.0-cp27-none-win_amd64.whl (1.3MB) 100% |################################| 1.3MB 393kB/s Installing collected packages: pillow Found existing installation: Pillow 2.8.0 Uninstalling Pillow-2.8.0: Successfully uninstalled Pillow-2.8.0 Successfully installed pillow-2.7.0 E:\stufftodelete>python test.py E:\stufftodelete>pip install pillow==2.6.0 Collecting pillow==2.6.0 Downloading Pillow-2.6.0-cp27-none-win_amd64.whl (1.3MB) 100% |################################| 1.3MB 405kB/s Installing collected packages: pillow Found existing installation: Pillow 2.7.0 Uninstalling Pillow-2.7.0: Successfully uninstalled Pillow-2.7.0 Successfully installed pillow-2.6.0 E:\stufftodelete>python test.pyPillow 2.7.0 was released on 1st January this year, and 2.8.0 was released on 1st April.
PR #1125 may have caused it. Ping @artscoop.
It'd be great to get this fixed for 2.9.0, due out on 1st July.
The good news is we now have a Windows-based CI to run unit tests, so if it's possible to simulate Alt+PrtSc (or just PrtSc, which also triggers it) on AppVeyor then this should definitely be unit-tested.
- addedBugAny unexpected behavior, until confirmed feature.Any unexpected behavior, until confirmed feature.
on Jun 22, 2015 Running OS X, I can reproduce the error using one of the files in the test suite - Tests/images/bmp/g/rgb32bf.bmp
Interestingly though, it's listed as unsupported.
Unfortunately, the ImageGrab functions don't work on the windows CI, likely because there's something screwy with the graphics context on a headless vm.
Notes:
This can be partially fixed by adding this
elifnear here:elif file_info['bits'] == 32 and file_info['rgb_mask'] == (0xff0000, 0xff00, 0xff): pass else: raise IOError("Unsupported BMP bitfields layout") else: raise IOError("Unsupported BMP bitfields layout")
That will return an image, but the resulting image is truncated. It can be shown or saved only if
LOAD_TRUNCATED_IMAGESis true, but the image is one pixel shifted left and missing the top line.This is because, for example,
self.tileis:
[('raw', (0, 0, 3360, 1050), 56L, ('BGRX', 13440, -1))]
instead of:
[('raw', (0, 0, 3360, 1050), 52L, ('BGRX', 13440, -1))]That third value in both cases is
offset or self.fp.tell(), the problem with 2.8.0 is that an extra read had been made to load in afile_info['a_mask'], which doesn't make sense for this format, and putting the file pointer at the wrong place.We should only be reading that alpha value when needed.
The regression appears with 3.0.0 too. 2.7.0 works ok.
So, any news on that?
Ok I understand what is going on.
I rewrote part of the BMP loading process.
And I made sure it worked on supported bitmap files, fixed bogus behaviour from previous versions, but it did not have to work with unsupported files.
However @radarhere , the cause of the error you've been experiencing with this file may be... let's say, different.Now I remember: the file rgb32bf.bmp is unsupported because it has a bitmask not supported by Pillow. Supported masks for 32-bit true color images have classic boundaries, with RGB channels in RGB or BGR order.
Here we have an esoteric RGB/A mask (the image is read correctly by XNView though), which has no matching dialect in PIL definitions for unpacking pixels.
Therefore I have been able to load this image, but with wrong colors (for instance, we have a problem with B and G channels)This bug can be closed. This works with 3.2.0:
>>> from PIL import ImageGrab >>> ImageGrab.grabclipboard() >>>It can be renamed "ImageGrab.grabclipboard() problem on Windows", because there are other issues there.
This bug is still present: I can reproduce it as described in the original report with Pillow 3.2.0 (Python 2.7, Windows 7).
34 remaining items
I need to dig a bit more about the actual definition of what a DIB file is, and what the data in the spot that would normally be an alpha channel actually corresponds to. I think that the fix is actually correct for the
RGBchannels, for what that's worth.BMP does not depend on any external library. (This particular version is essentially a header + raw image bytes)
Glad to hear that there are no external dependencies for BMP. I noticed the above problem because it seems that DIB is the internal bitmap representation for graphic shapes in OpenOffice (see XBitmap here).
Microsoft has an overview of DIBs here and a more historical piece here, and the actual “Bitmap Reference” is here. I hope this helps.
I've seen most of those links. What none of them are saying is the interpretation of the extra field of an RGBQuad, other than 'reserved'. There's possibly something newer that's done something documented with it, or we need to simply ignore it and just use the 'RGB' section of the image.
@wiredfool lets close this ticket and move the discussion to #1811. Because saved .bmp is fine. There might be a bug in conversion routines.
@wiredfool "IOError: Unsupported BMP bitfields layout" problem is fixed, and it becomes hard to follow this ticket.
Respinning this with #1847.
- added 2 commits that reference this issue
on Dec 13, 2016
Hello,
I have Windows 8 x64 system and installed Pillow version 2.8.2
I'm trying to grab image from clipboard. I press "Alt+PrtSc" and then call grabclipboard function, and get such error:
But if I call ImageGrab.grab function, screenshot(of whole screen) is saved properly.