Converted to unittest framework to deal with test framework error

This commit is contained in:
wiredfool 2014-06-23 16:52:10 -07:00
parent 6dce921dad
commit cf828f7612

View File

@ -1,164 +1,169 @@
# Test the ImageMorphology functionality # Test the ImageMorphology functionality
from tester import * from helper import *
from PIL import Image from PIL import Image
from PIL import ImageMorph from PIL import ImageMorph
def img_to_string(im):
"""Turn a (small) binary image into a string representation"""
chars = '.1'
width, height = im.size
return '\n'.join(
[''.join([chars[im.getpixel((c,r))>0] for c in range(width)])
for r in range(height)])
def string_to_img(image_string): class MorphTests(PillowTestCase):
"""Turn a string image representation into a binary image"""
rows = [s for s in image_string.replace(' ','').split('\n')
if len(s)]
height = len(rows)
width = len(rows[0])
im = Image.new('L',(width,height))
for i in range(width):
for j in range(height):
c = rows[j][i]
v = c in 'X1'
im.putpixel((i,j),v)
return im def setUp(self):
self.A = self.string_to_img(
def img_string_normalize(im): """
return img_to_string(string_to_img(im)) .......
.......
def assert_img_equal(A,B): ..111..
assert_equal(img_to_string(A), img_to_string(B)) ..111..
..111..
def assert_img_equal_img_string(A,Bstring): .......
assert_equal(img_to_string(A), img_string_normalize(Bstring)) .......
"""
A = string_to_img( )
"""
.......
.......
..111..
..111..
..111..
.......
.......
"""
)
def test_str_to_img():
im = Image.open('Tests/images/morph_a.png')
assert_image_equal(A, im)
def create_lut():
for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'):
lb = ImageMorph.LutBuilder(op_name=op)
lut = lb.build_lut()
with open('Tests/images/%s.lut' % op, 'wb') as f:
f.write(lut)
#create_lut()
def test_lut():
for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'):
lb = ImageMorph.LutBuilder(op_name=op)
lut = lb.build_lut()
with open('Tests/images/%s.lut' % op , 'rb') as f:
assert_equal(lut, bytearray(f.read()))
# Test the named patterns
def test_erosion8():
# erosion8
mop = ImageMorph.MorphOp(op_name='erosion8')
count,Aout = mop.apply(A)
assert_equal(count,8)
assert_img_equal_img_string(Aout,
"""
.......
.......
.......
...1...
.......
.......
.......
""")
def test_dialation8(): def img_to_string(self, im):
# dialation8 """Turn a (small) binary image into a string representation"""
mop = ImageMorph.MorphOp(op_name='dilation8') chars = '.1'
count,Aout = mop.apply(A) width, height = im.size
assert_equal(count,16) return '\n'.join(
assert_img_equal_img_string(Aout, [''.join([chars[im.getpixel((c,r))>0] for c in range(width)])
""" for r in range(height)])
.......
.11111.
.11111.
.11111.
.11111.
.11111.
.......
""")
def test_erosion4(): def string_to_img(self, image_string):
# erosion4 """Turn a string image representation into a binary image"""
mop = ImageMorph.MorphOp(op_name='dilation4') rows = [s for s in image_string.replace(' ','').split('\n')
count,Aout = mop.apply(A) if len(s)]
assert_equal(count,12) height = len(rows)
assert_img_equal_img_string(Aout, width = len(rows[0])
""" im = Image.new('L',(width,height))
....... for i in range(width):
..111.. for j in range(height):
.11111. c = rows[j][i]
.11111. v = c in 'X1'
.11111. im.putpixel((i,j),v)
..111..
.......
""")
def test_edge(): return im
# edge
mop = ImageMorph.MorphOp(op_name='edge') def img_string_normalize(self, im):
count,Aout = mop.apply(A) return self.img_to_string(self.string_to_img(im))
assert_equal(count,1)
assert_img_equal_img_string(Aout, def assert_img_equal(self, A, B):
""" self.assertEqual(self.img_to_string(A), self.img_to_string(B))
.......
....... def assert_img_equal_img_string(self, A, Bstring):
..111.. self.assertEqual(self.img_to_string(A), self.img_string_normalize(Bstring))
..1.1..
..111.. def test_str_to_img(self):
....... im = Image.open('Tests/images/morph_a.png')
....... self.assert_image_equal(self.A, im)
""")
def create_lut(self):
for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'):
lb = ImageMorph.LutBuilder(op_name=op)
lut = lb.build_lut(self)
with open('Tests/images/%s.lut' % op, 'wb') as f:
f.write(lut)
#create_lut()
def test_lut(self):
for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'):
lb = ImageMorph.LutBuilder(op_name=op)
lut = lb.build_lut()
with open('Tests/images/%s.lut' % op , 'rb') as f:
self.assertEqual(lut, bytearray(f.read()))
def test_corner(): # Test the named patterns
# Create a corner detector pattern def test_erosion8(self):
mop = ImageMorph.MorphOp(patterns = ['1:(... ... ...)->0', # erosion8
'4:(00. 01. ...)->1']) mop = ImageMorph.MorphOp(op_name='erosion8')
count,Aout = mop.apply(A) count,Aout = mop.apply(self.A)
assert_equal(count,5) self.assertEqual(count,8)
assert_img_equal_img_string(Aout, self.assert_img_equal_img_string(Aout,
""" """
....... .......
....... .......
..1.1.. .......
....... ...1...
..1.1.. .......
....... .......
....... .......
""") """)
def test_dialation8(self):
# dialation8
mop = ImageMorph.MorphOp(op_name='dilation8')
count,Aout = mop.apply(self.A)
self.assertEqual(count,16)
self.assert_img_equal_img_string(Aout,
"""
.......
.11111.
.11111.
.11111.
.11111.
.11111.
.......
""")
def test_erosion4(self):
# erosion4
mop = ImageMorph.MorphOp(op_name='dilation4')
count,Aout = mop.apply(self.A)
self.assertEqual(count,12)
self.assert_img_equal_img_string(Aout,
"""
.......
..111..
.11111.
.11111.
.11111.
..111..
.......
""")
def test_edge(self):
# edge
mop = ImageMorph.MorphOp(op_name='edge')
count,Aout = mop.apply(self.A)
self.assertEqual(count,1)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..111..
..1.1..
..111..
.......
.......
""")
# Test the coordinate counting with the same operator def test_corner(self):
coords = mop.match(A) # Create a corner detector pattern
assert_equal(len(coords), 4) mop = ImageMorph.MorphOp(patterns = ['1:(... ... ...)->0',
assert_equal(tuple(coords), '4:(00. 01. ...)->1'])
((2,2),(4,2),(2,4),(4,4))) count,Aout = mop.apply(self.A)
self.assertEqual(count,5)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..1.1..
.......
..1.1..
.......
.......
""")
coords = mop.get_on_pixels(Aout) # Test the coordinate counting with the same operator
assert_equal(len(coords), 4) coords = mop.match(self.A)
assert_equal(tuple(coords), self.assertEqual(len(coords), 4)
((2,2),(4,2),(2,4),(4,4))) self.assertEqual(tuple(coords),
((2,2),(4,2),(2,4),(4,4)))
coords = mop.get_on_pixels(Aout)
self.assertEqual(len(coords), 4)
self.assertEqual(tuple(coords),
((2,2),(4,2),(2,4),(4,4)))