diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index e398445fa..950ee13a1 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -109,7 +109,11 @@ class BmpImageFile(ImageFile.ImageFile): for idx, mask in enumerate(['r_mask', 'g_mask', 'b_mask', 'a_mask']): file_info[mask] = i32(header_data[36+idx*4:40+idx*4]) else: - for mask in ['r_mask', 'g_mask', 'b_mask', 'a_mask']: + # 40 byte headers only have the three components in the bitfields masks, + # ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx + # See also https://github.com/python-pillow/Pillow/issues/1293 + file_info['a_mask'] = 0xff000000 + for mask in ['r_mask', 'g_mask', 'b_mask']: file_info[mask] = i32(read(4)) file_info['rgb_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask']) file_info['rgba_mask'] = (file_info['r_mask'], file_info['g_mask'], file_info['b_mask'], file_info['a_mask']) diff --git a/Tests/images/clipboard.dib b/Tests/images/clipboard.dib new file mode 100644 index 000000000..03dcab81d Binary files /dev/null and b/Tests/images/clipboard.dib differ diff --git a/Tests/images/clipboard_target.png b/Tests/images/clipboard_target.png new file mode 100644 index 000000000..46a696543 Binary files /dev/null and b/Tests/images/clipboard_target.png differ diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index 25f70139d..29d59ee44 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -70,6 +70,13 @@ class TestFileBmp(PillowTestCase): self.assertEqual(im.size, reloaded.size) self.assertEqual(reloaded.format, "JPEG") + def test_load_dib(self): + # test for #1293, Imagegrab returning Unsupported Bitfields Format + im = BmpImagePlugin.DibImageFile('Tests/images/clipboard.dib') + target = Image.open('Tests/images/clipboard_target.png') + self.assert_image_equal(im.convert('RGB'), target.convert('RGB')) + self.assert_image_similar(im, target, 1) + if __name__ == '__main__': unittest.main()