Skip to content

Problem with saving image from clipboard #1293

Description

@sallyruthstruik

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:

ImageGrab.grabclipboard()
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-2-c8274e888e6c> in <module>()
      1 from PIL import ImageGrab
      2 
----> 3 ImageGrab.grabclipboard()

C:\Anaconda\lib\site-packages\PIL\ImageGrab.pyc in grabclipboard()
     49         from PIL import BmpImagePlugin
     50         import io
---> 51         return BmpImagePlugin.DibImageFile(io.BytesIO(data))
     52     return data

C:\Anaconda\lib\site-packages\PIL\ImageFile.pyc in __init__(self, fp, filename)
     95 
     96         try:
---> 97             self._open()
     98         except IndexError as v:  # end of data
     99             if Image.DEBUG > 1:

C:\Anaconda\lib\site-packages\PIL\BmpImagePlugin.pyc in _open(self)
    204 
    205     def _open(self):
--> 206         self._bitmap()
    207 
    208 #

C:\Anaconda\lib\site-packages\PIL\BmpImagePlugin.pyc in _bitmap(self, header, offset)
    145                     raw_mode = MASK_MODES[(file_info['bits'], file_info['rgb_mask'])]
    146                 else:
--> 147                     raise IOError("Unsupported BMP bitfields layout")
    148             else:
    149                 raise IOError("Unsupported BMP bitfields layout")

IOError: Unsupported BMP bitfields layout

But if I call ImageGrab.grab function, screenshot(of whole screen) is saved properly.

Activity

  1. sallyruthstruik commented on Jun 20, 2015

    @sallyruthstruik
    Author

    UPD It's version problem, when I installed Pillow version 2.6.0 everything worked ok

  2. hugovk commented on Jun 22, 2015

    @hugovk
    Member

    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.py
    
  3. hugovk commented on Jun 22, 2015

    @hugovk
    Member

    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.

  4. added
    BugAny unexpected behavior, until confirmed feature.
    on Jun 22, 2015
  5. radarhere commented on Jun 22, 2015

    @radarhere
    Member

    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.

  6. wiredfool commented on Jun 22, 2015

    @wiredfool
    Member

    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.

  7. hugovk commented on Jun 25, 2015

    @hugovk
    Member

    Notes:

    This can be partially fixed by adding this elif near 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_IMAGES is true, but the image is one pixel shifted left and missing the top line.

    This is because, for example, self.tile is:
    [('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 a file_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.

  8. techtonik commented on Nov 6, 2015

    @techtonik
    Contributor

    The regression appears with 3.0.0 too. 2.7.0 works ok.

  9. techtonik commented on Dec 15, 2015

    @techtonik
    Contributor

    So, any news on that?

  10. artscoop commented on Dec 30, 2015

    @artscoop
    Contributor

    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.

  11. artscoop commented on Dec 30, 2015

    @artscoop
    Contributor

    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)

  12. techtonik commented on Apr 5, 2016

    @techtonik
    Contributor

    This bug can be closed. This works with 3.2.0:

    >>> from PIL import ImageGrab
    >>> ImageGrab.grabclipboard()
    >>>
    
  13. techtonik commented on Apr 5, 2016

    @techtonik
    Contributor

    It can be renamed "ImageGrab.grabclipboard() problem on Windows", because there are other issues there.

  14. hugovk commented on Apr 6, 2016

    @hugovk
    Member

    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).

  15. 34 remaining items

  16. wiredfool commented on Apr 14, 2016

    @wiredfool
    Member

    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 RGB channels, for what that's worth.

  17. wiredfool commented on Apr 14, 2016

    @wiredfool
    Member

    BMP does not depend on any external library. (This particular version is essentially a header + raw image bytes)

  18. jenstroeger commented on Apr 14, 2016

    @jenstroeger

    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.

  19. wiredfool commented on Apr 15, 2016

    @wiredfool
    Member

    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.

  20. techtonik commented on Apr 16, 2016

    @techtonik
    Contributor

    @wiredfool lets close this ticket and move the discussion to #1811. Because saved .bmp is fine. There might be a bug in conversion routines.

  21. wiredfool commented on Apr 19, 2016

    @wiredfool
    Member
  22. techtonik commented on Apr 19, 2016

    @techtonik
    Contributor

    @wiredfool "IOError: Unsupported BMP bitfields layout" problem is fixed, and it becomes hard to follow this ticket.

  23. added a commit that references this issue on Apr 19, 2016
  24. wiredfool commented on Apr 19, 2016

    @wiredfool
    Member

    Respinning this with #1847.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.Windows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions