Merge pull request #351 from wiredfool/numpy-tests

tests for img -> numpy.array
This commit is contained in:
Alex Clark ☺ 2013-09-30 15:27:58 -07:00
commit 6599fe09d9
2 changed files with 50 additions and 3 deletions

View File

@ -1,6 +1,7 @@
from tester import * from tester import *
from PIL import Image from PIL import Image
import struct
try: try:
import site import site
@ -65,10 +66,43 @@ def test_3d_array():
assert_image(Image.fromarray(a[:, :, 1]), "L", (10, 10)) assert_image(Image.fromarray(a[:, :, 1]), "L", (10, 10))
def _test_img_equals_nparray(img, np):
assert_equal(img.size, np.shape[0:2])
px = img.load()
for x in xrange(0, img.size[0], img.size[0]/10):
for y in xrange(0, img.size[1], img.size[1]/10):
assert_deep_equal(px[x,y], np[y,x])
def test_16bit(): def test_16bit():
img = Image.open('Tests/images/12bit.cropped.tif') img = Image.open('Tests/images/12bit.cropped.tif')
px = img.load()
np_img = numpy.array(img) np_img = numpy.array(img)
assert_equal(np_img.shape, (64,64)) _test_img_equals_nparray(img, np_img)
assert_equal(px[1,1],np_img[1,1]) assert_equal(np_img.dtype, numpy.dtype('uint16'))
def test_to_array():
def _to_array(mode, dtype):
img = lena(mode)
np_img = numpy.array(img)
_test_img_equals_nparray(img, np_img)
assert_equal(np_img.dtype, numpy.dtype(dtype))
modes = [("L", 'uint8'),
("I", 'int32'),
("F", 'float32'),
("RGB", 'uint8'),
("RGBA", 'uint8'),
("RGBX", 'uint8'),
("CMYK", 'uint8'),
("YCbCr", 'uint8'),
("I;16", 'uint16'),
("I;16B", '>u2'),
("I;16L", 'uint16'),
]
for mode in modes:
assert_no_exception(lambda: _to_array(*mode))

View File

@ -67,6 +67,19 @@ def assert_equal(a, b, msg=None):
else: else:
failure(msg or "got %r, expected %r" % (a, b)) failure(msg or "got %r, expected %r" % (a, b))
def assert_deep_equal(a, b, msg=None):
try:
if len(a) == len(b):
if all([x==y for x,y in zip(a,b)]):
success()
else:
failure(msg or "got %s, expected %s" % (a,b))
else:
failure(msg or "got length %s, expected %s" % (len(a), len(b)))
except:
assert_equal(a,b,msg)
def assert_match(v, pattern, msg=None): def assert_match(v, pattern, msg=None):
import re import re
if re.match(pattern, v): if re.match(pattern, v):