BUG: io: imread in the PIL plugin drops indexed PNG alpha channel.

When using the PIL plugin to read an indexed PNG file that has
an alpha channel, the alpha channel would be lost.
This commit is contained in:
Warren Weckesser
2015-11-18 13:55:57 -05:00
parent 2f3bc7f08d
commit 6b908c1bb1
3 changed files with 26 additions and 1 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 116 B

+4 -1
View File
@@ -85,7 +85,10 @@ def pil_to_ndarray(im, dtype=None, img_num=None):
if grayscale:
frame = im.convert('L')
else:
frame = im.convert('RGB')
if im.format == 'PNG' and 'transparency' in im.info:
frame = im.convert('RGBA')
else:
frame = im.convert('RGB')
elif im.mode == '1':
frame = im.convert('L')
+22
View File
@@ -84,6 +84,28 @@ def test_imread_palette():
assert img.ndim == 3
def test_imread_index_png_with_alpha():
# The file `foo3x5x4indexed.png` was created with this array
# (3x5 is (height)x(width)):
data = np.array([[[127, 0, 255, 255],
[127, 0, 255, 255],
[127, 0, 255, 255],
[127, 0, 255, 255],
[127, 0, 255, 255]],
[[192, 192, 255, 0],
[192, 192, 255, 0],
[0, 0, 255, 0],
[0, 0, 255, 0],
[0, 0, 255, 0]],
[[0, 31, 255, 255],
[0, 31, 255, 255],
[0, 31, 255, 255],
[0, 31, 255, 255],
[0, 31, 255, 255]]], dtype=np.uint8)
img = imread(os.path.join(data_dir, 'foo3x5x4indexed.png'))
assert_array_equal(img, data)
def test_palette_is_gray():
gray = Image.open(os.path.join(data_dir, 'palette_gray.png'))
assert _palette_is_grayscale(gray)