Merge pull request #2683 from wiredfool/issue_2044

Test/fix for issue #2044
This commit is contained in:
wiredfool 2017-08-17 11:52:07 +01:00 committed by GitHub
commit e1aba29f9b
3 changed files with 27 additions and 5 deletions

View File

@ -2423,7 +2423,7 @@ def fromqpixmap(im):
_fromarray_typemap = {
# (shape, typestr) => mode, rawmode
# first two members of shape are set to one
# ((1, 1), "|b1"): ("1", "1"), # broken
((1, 1), "|b1"): ("1", "1;8"),
((1, 1), "|u1"): ("L", "L"),
((1, 1), "|i1"): ("I", "I;8"),
((1, 1), "<u2"): ("I", "I;16"),

View File

@ -39,7 +39,7 @@ class TestNumpy(PillowTestCase):
def to_image(dtype, bands=1, boolean=0):
if bands == 1:
if boolean:
data = [0, 1] * 50
data = [0, 255] * 50
else:
data = list(range(100))
a = numpy.array(data, dtype=dtype)
@ -58,9 +58,9 @@ class TestNumpy(PillowTestCase):
return i
# Check supported 1-bit integer formats
self.assertRaises(TypeError, lambda: to_image(numpy.bool))
self.assertRaises(TypeError, lambda: to_image(numpy.bool8))
self.assert_image(to_image(numpy.bool, 1, 1), '1', TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.bool8, 1, 1), '1', TEST_IMAGE_SIZE)
# Check supported 8-bit integer formats
self.assert_image(to_image(numpy.uint8), "L", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 3), "RGB", TEST_IMAGE_SIZE)
@ -213,5 +213,13 @@ class TestNumpy(PillowTestCase):
self.assertEqual(im.size, (0, 0))
def test_bool(self):
# https://github.com/python-pillow/Pillow/issues/2044
a = numpy.zeros((10,2), dtype=numpy.bool)
a[0][0] = True
im2 = Image.fromarray(a)
self.assertEqual(im2.getdata()[0], 255)
if __name__ == '__main__':
unittest.main()

View File

@ -185,6 +185,19 @@ unpack1IR(UINT8* out, const UINT8* in, int pixels)
}
}
static void
unpack18(UINT8* out, const UINT8* in, int pixels)
{
/* Unpack a '|b1' image, which is a numpy boolean.
1 == true, 0==false, in bytes */
int i;
for (i = 0; i < pixels; i++) {
out[i] = in[i] > 0 ? 255 : 0;
}
}
/* Unpack to "L" image */
@ -1168,6 +1181,7 @@ static struct {
{"1", "1;I", 1, unpack1I},
{"1", "1;R", 1, unpack1R},
{"1", "1;IR", 1, unpack1IR},
{"1", "1;8", 1, unpack18},
/* greyscale */
{"L", "L;2", 2, unpackL2},