Skip to content

Commit

Permalink
Merge 824a0c2 into f12febf
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-brett committed Aug 18, 2016
2 parents f12febf + 824a0c2 commit 9b410a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
26 changes: 15 additions & 11 deletions PIL/Image.py
Expand Up @@ -246,7 +246,7 @@ def isImageType(t):

_MODE_CONV = {
# official modes
"1": ('|b1', None), # broken
"1": ('|b1', None), # Bits need to be extended to bytes
"L": ('|u1', None),
"LA": ('|u1', 2),
"I": (_ENDIAN + 'i4', None),
Expand Down Expand Up @@ -615,17 +615,21 @@ def _repr_png_(self):
self.save(b, 'PNG')
return b.getvalue()

def __getattr__(self, name):
if name == "__array_interface__":
# numpy array interface support
new = {}
shape, typestr = _conv_type_shape(self)
new['shape'] = shape
new['typestr'] = typestr
@property
def __array_interface__(self):
# numpy array interface support
new = {}
shape, typestr = _conv_type_shape(self)
new['shape'] = shape
new['typestr'] = typestr
new['version'] = 3
if self.mode == '1':
# Binary images need to be extended from bits to bytes
# See: https://github.com/python-pillow/Pillow/issues/350
new['data'] = self.tobytes('raw', 'L')
else:
new['data'] = self.tobytes()
new['version'] = 3
return new
raise AttributeError(name)
return new

def __getstate__(self):
return [
Expand Down
19 changes: 16 additions & 3 deletions Tests/test_image_array.py
Expand Up @@ -25,13 +25,26 @@ def test(mode):
self.assertEqual(test("RGBX"), (3, (100, 128, 4), '|u1', 51200))

def test_fromarray(self):

class Wrapper(object):
""" Class with API matching Image.fromarray """

def __init__(self, img, arr_params):
self.img = img
self.__array_interface__ = arr_params

def tobytes(self):
return self.img.tobytes()

def test(mode):
i = im.convert(mode)
a = i.__array_interface__
a["strides"] = 1 # pretend it's non-contiguous
i.__array_interface__ = a # patch in new version of attribute
out = Image.fromarray(i)
a["strides"] = 1 # pretend it's non-contigous
# Make wrapper instance for image, new array interface
wrapped = Wrapper(i, a)
out = Image.fromarray(wrapped)
return out.mode, out.size, list(i.getdata()) == list(out.getdata())

# self.assertEqual(test("1"), ("1", (128, 100), True))
self.assertEqual(test("L"), ("L", (128, 100), True))
self.assertEqual(test("I"), ("I", (128, 100), True))
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_numpy.py
Expand Up @@ -117,6 +117,15 @@ def test_16bit(self):
self._test_img_equals_nparray(img, np_img)
self.assertEqual(np_img.dtype, numpy.dtype('<u2'))

def test_1bit(self):
# Test that 1-bit arrays convert to numpy and back
# See: https://github.com/python-pillow/Pillow/issues/350
arr = numpy.array([[1, 0, 0, 1, 0], [0, 1, 0, 0, 0]], 'u1')
img = Image.fromarray(arr * 255).convert('1')
self.assertEqual(img.mode, '1')
arr_back = numpy.array(img)
numpy.testing.assert_array_equal(arr, arr_back)

def test_save_tiff_uint16(self):
'''
Open a single-channel uint16 greyscale image and verify that it can be saved without
Expand Down

0 comments on commit 9b410a4

Please sign in to comment.