Move constant to module level.

This commit is contained in:
Bryant Mairs 2016-04-18 16:58:21 -07:00 committed by wiredfool
parent 7d5856bdf7
commit 4f2836e218

View File

@ -10,9 +10,9 @@ except ImportError:
# Skip via setUp()
pass
TEST_IMAGE_SIZE = (10, 10)
class TestNumpy(PillowTestCase):
TEST_IMAGE_SIZE = (10, 10)
def setUp(self):
try:
@ -30,14 +30,14 @@ class TestNumpy(PillowTestCase):
else:
data = list(range(100))
a = numpy.array(data, dtype=dtype)
a.shape = TestNumpy.TEST_IMAGE_SIZE
a.shape = TEST_IMAGE_SIZE
i = Image.fromarray(a)
if list(i.getdata()) != data:
print("data mismatch for", dtype)
else:
data = list(range(100))
a = numpy.array([[x]*bands for x in data], dtype=dtype)
a.shape = TestNumpy.TEST_IMAGE_SIZE[0], TestNumpy.TEST_IMAGE_SIZE[1], bands
a.shape = TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], bands
i = Image.fromarray(a)
if list(i.split()[0].getdata()) != list(range(100)):
print("data mismatch for", dtype)
@ -49,44 +49,44 @@ class TestNumpy(PillowTestCase):
self.assertRaises(TypeError, lambda: to_image(numpy.bool8))
# Check supported 8-bit integer formats
self.assert_image(to_image(numpy.uint8), "L", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 3), "RGB", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 4), "RGBA", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int8), "I", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8), "L", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 3), "RGB", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 4), "RGBA", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int8), "I", TEST_IMAGE_SIZE)
# Check non-fixed-size integer types
self.assert_image(to_image(numpy.uint), "I", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int), "I", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint), "I", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int), "I", TEST_IMAGE_SIZE)
# Check 16-bit integer formats
if Image._ENDIAN == '<':
self.assert_image(to_image(numpy.uint16), "I;16", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint16), "I;16", TEST_IMAGE_SIZE)
else:
self.assert_image(to_image(numpy.uint16), "I;16B", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint16), "I;16B", TEST_IMAGE_SIZE)
if Image._ENDIAN == '<':
self.assert_image(to_image(numpy.int16), "I;16S", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int16), "I;16S", TEST_IMAGE_SIZE)
else:
self.assert_image(to_image(numpy.int16), "I;16BS", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int16), "I;16BS", TEST_IMAGE_SIZE)
# Check 32-bit integer formats
if Image._ENDIAN == '<':
self.assert_image(to_image(numpy.uint32), "I;32", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint32), "I;32", TEST_IMAGE_SIZE)
else:
self.assert_image(to_image(numpy.uint32), "I;32B", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint32), "I;32B", TEST_IMAGE_SIZE)
if Image._ENDIAN == '<':
self.assert_image(to_image(numpy.int32), "I;32S", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int32), "I;32S", TEST_IMAGE_SIZE)
else:
self.assert_image(to_image(numpy.int32), "I;32BS", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.int32), "I;32BS", TEST_IMAGE_SIZE)
# Check 64-bit integer formats
self.assertRaises(TypeError, lambda: to_image(numpy.uint64))
self.assertRaises(TypeError, lambda: to_image(numpy.int64))
# Check floating-point formats
self.assert_image(to_image(numpy.float), "F", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float), "F", TEST_IMAGE_SIZE)
self.assertRaises(TypeError, lambda: to_image(numpy.float16))
self.assert_image(to_image(numpy.float32), "F", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float64), "F", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float32), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.float64), "F", TEST_IMAGE_SIZE)
self.assert_image(to_image(numpy.uint8, 2), "LA", (10, 10))
self.assert_image(to_image(numpy.uint8, 3), "RGB", (10, 10))
@ -95,15 +95,15 @@ class TestNumpy(PillowTestCase):
# based on an erring example at
# http://stackoverflow.com/questions/10854903/what-is-causing-dimension-dependent-attributeerror-in-pil-fromarray-function
def test_3d_array(self):
size = (5, TestNumpy.TEST_IMAGE_SIZE[0], TestNumpy.TEST_IMAGE_SIZE[1])
size = (5, TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1])
a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[1, :, :]), "L", TestNumpy.TEST_IMAGE_SIZE)
size = (TestNumpy.TEST_IMAGE_SIZE[0], 5, TestNumpy.TEST_IMAGE_SIZE[1])
self.assert_image(Image.fromarray(a[1, :, :]), "L", TEST_IMAGE_SIZE)
size = (TEST_IMAGE_SIZE[0], 5, TEST_IMAGE_SIZE[1])
a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[:, 1, :]), "L", TestNumpy.TEST_IMAGE_SIZE)
size = (TestNumpy.TEST_IMAGE_SIZE[0], TestNumpy.TEST_IMAGE_SIZE[1], 5)
self.assert_image(Image.fromarray(a[:, 1, :]), "L", TEST_IMAGE_SIZE)
size = (TEST_IMAGE_SIZE[0], TEST_IMAGE_SIZE[1], 5)
a = numpy.ones(size, dtype=numpy.uint8)
self.assert_image(Image.fromarray(a[:, :, 1]), "L", TestNumpy.TEST_IMAGE_SIZE)
self.assert_image(Image.fromarray(a[:, :, 1]), "L", TEST_IMAGE_SIZE)
def _test_img_equals_nparray(self, img, np):
np_size = np.shape[1], np.shape[0]
@ -127,8 +127,8 @@ class TestNumpy(PillowTestCase):
tmpfile = self.tempfile("temp.tif")
pixel_value = 0x1234
filename = "Tests/images/uint16_1_4660.tif"
a = numpy.array([pixel_value] * 100, dtype=numpy.uint16)
a.shape = TestNumpy.TEST_IMAGE_SIZE
a = numpy.array([pixel_value] * TEST_IMAGE_SIZE[0] * TEST_IMAGE_SIZE[1], dtype=numpy.uint16)
a.shape = TEST_IMAGE_SIZE
Image.fromarray(a).save(tmpfile)
im_test = Image.open(tmpfile)
im_good = Image.open(filename)