Pillow/Tests/test_imagemorph.py

176 lines
6.1 KiB
Python
Raw Normal View History

# Test the ImageMorphology functionality
2014-08-28 15:44:19 +04:00
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import ImageMorph
class MorphTests(PillowTestCase):
def setUp(self):
self.A = self.string_to_img(
2014-06-24 10:34:05 +04:00
"""
.......
.......
..111..
..111..
..111..
.......
.......
"""
2014-06-24 10:34:05 +04:00
)
def img_to_string(self, im):
"""Turn a (small) binary image into a string representation"""
chars = '.1'
width, height = im.size
return '\n'.join(
2014-06-24 10:34:05 +04:00
[''.join([chars[im.getpixel((c, r)) > 0] for c in range(width)])
for r in range(height)])
def string_to_img(self, image_string):
"""Turn a string image representation into a binary image"""
2014-06-24 10:34:05 +04:00
rows = [s for s in image_string.replace(' ', '').split('\n')
if len(s)]
height = len(rows)
width = len(rows[0])
2014-06-24 10:34:05 +04:00
im = Image.new('L', (width, height))
for i in range(width):
for j in range(height):
c = rows[j][i]
v = c in 'X1'
2014-06-24 10:34:05 +04:00
im.putpixel((i, j), v)
return im
def img_string_normalize(self, im):
return self.img_to_string(self.string_to_img(im))
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):
2014-06-24 10:34:05 +04:00
self.assertEqual(
self.img_to_string(A),
self.img_string_normalize(Bstring))
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):
2014-06-24 10:34:05 +04:00
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)
2014-06-24 10:34:05 +04:00
# create_lut()
def test_lut(self):
2014-06-24 10:34:05 +04:00
for op in (
'corner', 'dilation4', 'dilation8',
'erosion4', 'erosion8', 'edge'):
lb = ImageMorph.LutBuilder(op_name=op)
lut = lb.build_lut()
2014-06-24 10:34:05 +04:00
with open('Tests/images/%s.lut' % op, 'rb') as f:
self.assertEqual(lut, bytearray(f.read()))
# Test the named patterns
def test_erosion8(self):
# erosion8
mop = ImageMorph.MorphOp(op_name='erosion8')
2014-06-24 10:34:05 +04:00
count, Aout = mop.apply(self.A)
self.assertEqual(count, 8)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
.......
...1...
.......
.......
.......
""")
def test_dialation8(self):
# dialation8
mop = ImageMorph.MorphOp(op_name='dilation8')
2014-06-24 10:34:05 +04:00
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')
2014-06-24 10:34:05 +04:00
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):
2014-06-24 10:34:05 +04:00
# edge
mop = ImageMorph.MorphOp(op_name='edge')
2014-06-24 10:34:05 +04:00
count, Aout = mop.apply(self.A)
self.assertEqual(count, 1)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..111..
..1.1..
..111..
.......
.......
""")
def test_corner(self):
# Create a corner detector pattern
2014-06-24 10:34:05 +04:00
mop = ImageMorph.MorphOp(patterns=['1:(... ... ...)->0',
'4:(00. 01. ...)->1'])
count, Aout = mop.apply(self.A)
self.assertEqual(count, 5)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..1.1..
.......
..1.1..
.......
.......
""")
# Test the coordinate counting with the same operator
coords = mop.match(self.A)
self.assertEqual(len(coords), 4)
2014-06-24 10:34:05 +04:00
self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))
coords = mop.get_on_pixels(Aout)
self.assertEqual(len(coords), 4)
2014-06-24 10:34:05 +04:00
self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))
2014-06-24 11:01:43 +04:00
if __name__ == '__main__':
unittest.main()
2014-06-24 10:34:05 +04:00
# End of file