From 761081b1e32d995cdab6667c1dc1ba99b526948d Mon Sep 17 00:00:00 2001 From: hugovk Date: Sun, 28 May 2017 22:56:25 +0300 Subject: [PATCH 1/4] Test mirroring --- Tests/test_imagemorph.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index f2247bdca..c0f6448e6 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -176,6 +176,24 @@ class MorphTests(PillowTestCase): self.assertEqual(len(coords), 4) self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4))) + def test_mirroring(self): + # Test 'M' for mirroring + mop = ImageMorph.MorphOp(patterns=['1:(... ... ...)->0', + 'M:(00. 01. ...)->1']) + count, Aout = mop.apply(self.A) + self.assertEqual(count, 7) + self.assert_img_equal_img_string(Aout, + """ + + ....... + ....... + ..1.1.. + ....... + ....... + ....... + ....... + """) + def test_non_binary_images(self): im = hopper('RGB') mop = ImageMorph.MorphOp(op_name="erosion8") From 04f28b691e961bff44b269652421f447add04c5c Mon Sep 17 00:00:00 2001 From: hugovk Date: Sun, 28 May 2017 23:02:06 +0300 Subject: [PATCH 2/4] Test negate and fix a TypeError: list indices must be integers, not str --- PIL/ImageMorph.py | 6 +++--- Tests/test_imagemorph.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/PIL/ImageMorph.py b/PIL/ImageMorph.py index dcce63fa4..d2367737f 100644 --- a/PIL/ImageMorph.py +++ b/PIL/ImageMorph.py @@ -123,7 +123,7 @@ class LutBuilder(object): .replace('0', 'Z') .replace('1', '0') .replace('Z', '1')) - res = '%d' % (1-int(res)) + res = 1-int(res) patterns.append((pattern, res)) return patterns @@ -152,8 +152,8 @@ class LutBuilder(object): patterns += self._pattern_permute(pattern, options, result) # # Debugging -# for p,r in patterns: -# print(p,r) +# for p, r in patterns: +# print(p, r) # print('--') # compile the patterns into regular expressions for speed diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index c0f6448e6..dac1ae150 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -184,7 +184,6 @@ class MorphTests(PillowTestCase): self.assertEqual(count, 7) self.assert_img_equal_img_string(Aout, """ - ....... ....... ..1.1.. @@ -194,6 +193,23 @@ class MorphTests(PillowTestCase): ....... """) + def test_negate(self): + # Test 'N' for negate + mop = ImageMorph.MorphOp(patterns=['1:(... ... ...)->0', + 'N:(00. 01. ...)->1']) + count, Aout = mop.apply(self.A) + self.assertEqual(count, 8) + self.assert_img_equal_img_string(Aout, + """ + ....... + ....... + ..1.... + ....... + ....... + ....... + ....... + """) + def test_non_binary_images(self): im = hopper('RGB') mop = ImageMorph.MorphOp(op_name="erosion8") From 4c86ef0b521a9d8555de8e11445bf31cf038537f Mon Sep 17 00:00:00 2001 From: hugovk Date: Mon, 29 May 2017 08:29:12 +0300 Subject: [PATCH 3/4] More morph tests --- Tests/test_imagemorph.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index dac1ae150..ef9d38fd4 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -218,6 +218,61 @@ class MorphTests(PillowTestCase): self.assertRaises(Exception, lambda: mop.match(im)) self.assertRaises(Exception, lambda: mop.get_on_pixels(im)) + def test_add_patterns(self): + # Arrange + lb = ImageMorph.LutBuilder(op_name='corner') + self.assertEqual(lb.patterns, ['1:(... ... ...)->0', + '4:(00. 01. ...)->1']) + new_patterns = ['M:(00. 01. ...)->1', + 'N:(00. 01. ...)->1'] + + # Act + lb.add_patterns(new_patterns) + + # Assert + self.assertEqual( + lb.patterns, + ['1:(... ... ...)->0', + '4:(00. 01. ...)->1', + 'M:(00. 01. ...)->1', + 'N:(00. 01. ...)->1']) + + def test_unknown_pattern(self): + self.assertRaises( + Exception, + lambda: ImageMorph.LutBuilder(op_name='unknown')) + + def test_pattern_syntax_error(self): + # Arrange + lb = ImageMorph.LutBuilder(op_name='corner') + new_patterns = ['a pattern with a syntax error'] + lb.add_patterns(new_patterns) + + # Act / Assert + self.assertRaises( + Exception, + lambda: lb.build_lut()) + + def test_load_invalid_mrl(self): + # Arrange + invalid_mrl = 'Tests/images/hopper.png' + mop = ImageMorph.MorphOp() + + # Act / Assert + self.assertRaises(Exception, lambda: mop.load_lut(invalid_mrl)) + + def test_set_lut(self): + # Arrange + lb = ImageMorph.LutBuilder(op_name='corner') + lut = lb.build_lut() + mop = ImageMorph.MorphOp() + + # Act + mop.set_lut(lut) + + # Assert + self.assertEqual(mop.lut, lut) + if __name__ == '__main__': unittest.main() From ffcd945ae7f96452787057f6869a2b20640f275d Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 29 May 2017 09:52:31 +0300 Subject: [PATCH 4/4] Test MRL save/load roundtrip --- Tests/test_imagemorph.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index ef9d38fd4..0ce8ac6da 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -261,6 +261,19 @@ class MorphTests(PillowTestCase): # Act / Assert self.assertRaises(Exception, lambda: mop.load_lut(invalid_mrl)) + def test_roundtrip_mrl(self): + # Arrange + tempfile = self.tempfile('temp.mrl') + mop = ImageMorph.MorphOp(op_name='corner') + initial_lut = mop.lut + + # Act + mop.save_lut(tempfile) + mop.load_lut(tempfile) + + # Act / Assert + self.assertEqual(mop.lut, initial_lut) + def test_set_lut(self): # Arrange lb = ImageMorph.LutBuilder(op_name='corner')