diff --git a/Tests/images/color_snakes.png b/Tests/images/color_snakes.png new file mode 100644 index 000000000..bf3a35196 Binary files /dev/null and b/Tests/images/color_snakes.png differ diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index ba65f211b..d1bdacd52 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -5,6 +5,7 @@ from PIL import ImageOps from PIL import ImageFilter im = Image.open("Tests/images/hopper.ppm") +snakes = Image.open("Tests/images/color_snakes.png") class TestImageOpsUsm(PillowTestCase): @@ -16,7 +17,7 @@ class TestImageOpsUsm(PillowTestCase): self.assertEqual(i.size, (128, 128)) # i.save("blur.bmp") - i = ImageOps.usm(im, 2.0, 125, 8) + i = ImageOps.unsharp_mask(im, 2.0, 125, 8) self.assertEqual(i.mode, "RGB") self.assertEqual(i.size, (128, 128)) # i.save("usm.bmp") @@ -33,7 +34,7 @@ class TestImageOpsUsm(PillowTestCase): self.assertEqual(i.mode, "RGB") self.assertEqual(i.size, (128, 128)) - def test_usm(self): + def test_usm_formats(self): usm = ImageOps.unsharp_mask self.assertRaises(ValueError, lambda: usm(im.convert("1"))) @@ -45,7 +46,7 @@ class TestImageOpsUsm(PillowTestCase): usm(im.convert("CMYK")) self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) - def test_blur(self): + def test_blur_formats(self): blur = ImageOps.gaussian_blur self.assertRaises(ValueError, lambda: blur(im.convert("1"))) @@ -57,6 +58,33 @@ class TestImageOpsUsm(PillowTestCase): blur(im.convert("CMYK")) self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) + def test_usm_accuracy(self): + + i = snakes._new(ImageOps.unsharp_mask(snakes, 5, 1024, 0)) + # Image should not be changed because it have only 0 and 255 levels. + self.assertEqual(i.tobytes(), snakes.tobytes()) + + def test_blur_accuracy(self): + + i = snakes._new(ImageOps.gaussian_blur(snakes, .9)) + # Alpha channel must match whole. + self.assertEqual(i.split()[3], snakes.split()[3]) + # These pixels surrounded with pixels with 255 intensity. + # They must be 255. + for x, y, c in [(1, 0, 1), (2, 0, 1), (7, 8, 1), (8, 8, 1), (2, 9, 1), + (7, 3, 0), (8, 3, 0), (5, 8, 0), (5, 9, 0), (1, 3, 0), + (4, 3, 2), (4, 2, 2)]: + self.assertEqual(i.im.getpixel((x, y))[c], 255) + # Fuzzy match. + gp = lambda x, y: i.im.getpixel((x, y)) + self.assertTrue(212 <= gp(7, 4)[0] <= 214) + self.assertTrue(212 <= gp(7, 5)[2] <= 214) + self.assertTrue(212 <= gp(7, 6)[2] <= 214) + self.assertTrue(212 <= gp(7, 7)[1] <= 214) + self.assertTrue(212 <= gp(8, 4)[0] <= 214) + self.assertTrue(212 <= gp(8, 5)[2] <= 214) + self.assertTrue(212 <= gp(8, 6)[2] <= 214) + self.assertTrue(212 <= gp(8, 7)[1] <= 214) if __name__ == '__main__': unittest.main()