2014-09-05 14:03:56 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper().resize((128, 100))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-07-19 21:21:18 +04:00
|
|
|
class TestImageArray(PillowTestCase):
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_toarray(self):
|
|
|
|
def test(mode):
|
|
|
|
ai = im.convert(mode).__array_interface__
|
2015-11-02 21:07:12 +03:00
|
|
|
return ai['version'], ai["shape"], ai["typestr"], len(ai["data"])
|
|
|
|
# self.assertEqual(test("1"), (3, (100, 128), '|b1', 1600))
|
|
|
|
self.assertEqual(test("L"), (3, (100, 128), '|u1', 12800))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# FIXME: wrong?
|
2015-11-02 21:07:12 +03:00
|
|
|
self.assertEqual(test("I"), (3, (100, 128), Image._ENDIAN + 'i4', 51200))
|
2014-06-10 13:10:47 +04:00
|
|
|
# FIXME: wrong?
|
2015-11-02 21:07:12 +03:00
|
|
|
self.assertEqual(test("F"), (3, (100, 128), Image._ENDIAN + 'f4', 51200))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2016-04-28 17:30:43 +03:00
|
|
|
self.assertEqual(test("LA"), (3, (100, 128, 2), '|u1', 25600))
|
2015-11-02 21:07:12 +03:00
|
|
|
self.assertEqual(test("RGB"), (3, (100, 128, 3), '|u1', 38400))
|
|
|
|
self.assertEqual(test("RGBA"), (3, (100, 128, 4), '|u1', 51200))
|
|
|
|
self.assertEqual(test("RGBX"), (3, (100, 128, 4), '|u1', 51200))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
def test_fromarray(self):
|
2016-08-08 01:25:43 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test(mode):
|
|
|
|
i = im.convert(mode)
|
|
|
|
a = i.__array_interface__
|
2016-08-31 13:12:07 +03:00
|
|
|
a["strides"] = 1 # pretend it's non-contiguous
|
2016-08-08 01:25:43 +03:00
|
|
|
# Make wrapper instance for image, new array interface
|
|
|
|
wrapped = Wrapper(i, a)
|
|
|
|
out = Image.fromarray(wrapped)
|
2014-06-10 13:10:47 +04:00
|
|
|
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
|
2016-08-08 01:25:43 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# self.assertEqual(test("1"), ("1", (128, 100), True))
|
|
|
|
self.assertEqual(test("L"), ("L", (128, 100), True))
|
|
|
|
self.assertEqual(test("I"), ("I", (128, 100), True))
|
|
|
|
self.assertEqual(test("F"), ("F", (128, 100), True))
|
2016-04-28 17:30:43 +03:00
|
|
|
self.assertEqual(test("LA"), ("LA", (128, 100), True))
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(test("RGB"), ("RGB", (128, 100), True))
|
|
|
|
self.assertEqual(test("RGBA"), ("RGBA", (128, 100), True))
|
|
|
|
self.assertEqual(test("RGBX"), ("RGBA", (128, 100), True))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|