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
Problem with saving image from clipboard #1293
Comments
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.
|
Pillow 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. |
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 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 This is because, for example, That third value in both cases is 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. |
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. |
This bug can be closed. This works with 3.2.0:
|
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). |
@hugovk can you confirm it with the following actions? 1 open mspaint from PIL import ImageGrab
im = ImageGrab.grabclipboard()
if im is None:
print('Error: No image data in clipboard') Python 2.7, 32-bit Vista, Pillow 3.2.0 works. |
|
@hugovk yep, the same here, so looks like this issue is only about console window. |
What is the bitfield layout? If it's sane, there's no reason that we can't support it. |
@wiredfool any ideas where to look for it? Let me update the Traceback for 3.2.0 first. Traceback (most recent call last):
File "code.py", line 2, in <module>
im = ImageGrab.grabclipboard()
File "C:\Python27\lib\site-packages\PIL\ImageGrab.py", line 60, 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 211, in _open
self._bitmap()
File "C:\Python27\lib\site-packages\PIL\BmpImagePlugin.py", line 152, in _bitmap
raise IOError("Unsupported BMP bitfields layout")
IOError: Unsupported BMP bitfields layout |
Printing {
'direction': -1,
'r_mask': 16711680,
'compression': 3,
'pixels_per_meter': (0, 0),
'palette_padding': 4,
'g_mask': 65280,
'a_mask': 6908265,
'rgba_mask': (16711680, 65280, 255, 6908265),
'header_size': 40,
'height': 736,
'width': 1066,
'colors': 4294967296L,
'b_mask': 255,
'rgb_mask': (16711680, 65280, 255),
'planes': 1,
'y_flip': False,
'bits': 32,
'data_size': 3138304,
} |
ok, that's a WTF. The alpha mask is 0x696969, so it's a BRG[WTF] image?? |
Try this:
|
The mask of the image is:
while supported ones for 32 bits are:
I can't find any references of that magic number. Maybe Pillow gets that number wrong? |
That magic number makes no sense at all from a masking point of view. |
From https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx , in the section on the 40 byte headers, which is the type that we're using here:
So, when we have a 40 byte header, we can't read the 4 components of the mask. |
Ok, I think this patch should do it, where the previous one would only work if the first pixel color was 0x696969.
|
Hmm. Looks like there is an alpha channel involved, and it's inverted -- e.g. 00 is solid and FF is transparent. |
…ansparency mask
This patch works too. |
The DIB image format uses the 40 byte BMP header, followed by 3 masks for RGB channels. We were reading 4 masks, consuming the first pixel of the image. Mostly fixes issue python-pillow#1293. Remaining issue: alpha channel is actually a transparency mask.
I can confirm this is fixed for me. |
I haven't built this from source. When will the DIB/DMP fix make it into the Python module and become available through pip? Interestingly, Pillow 3.0.0 works fine on Mac/MacPorts, yet Pillow 3.2.0 fails on Linux with the above issue. BMP does not have a dependency to a particular host library, does it? The current traceback for Pillow 3.2.0 on my Linux host is as follows, where the input byte array
Applying change wiredfool@e84e029 I can confirm it works. So I guess I'll just wait for the next release cycle. |
Releases are quarterly; next one is due for 1st July. |
Note that the current fix isn't actually correct, I believe that it's misinterpreting some active mask as transparency. |
Ah. I noticed black backgrounds in bmp to png converted images, perhaps that'd explain them... 🤓 |
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 |
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. |
The DIB image format uses the 40 byte BMP header, followed by 3 masks for RGB channels. We were reading 4 masks, consuming the first pixel of the image. Mostly fixes issue python-pillow#1293. Remaining issue: alpha channel is actually a transparency mask.
Respinning this with #1847. |
The DIB image format uses the 40 byte BMP header, followed by 3 masks for RGB channels. We were reading 4 masks, consuming the first pixel of the image. Mostly fixes issue python-pillow#1293. Remaining issue: alpha channel is actually a transparency mask.
The DIB image format uses the 40 byte BMP header, followed by 3 masks for RGB channels. We were reading 4 masks, consuming the first pixel of the image. Mostly fixes issue python-pillow#1293. Remaining issue: alpha channel is actually a transparency mask.
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.
The text was updated successfully, but these errors were encountered: