add detailed test for blur and simple test for sharp

This commit is contained in:
homm 2014-10-08 06:49:56 +04:00
parent 05c99131e0
commit ddae12928a
2 changed files with 31 additions and 3 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -5,6 +5,7 @@ from PIL import ImageOps
from PIL import ImageFilter from PIL import ImageFilter
im = Image.open("Tests/images/hopper.ppm") im = Image.open("Tests/images/hopper.ppm")
snakes = Image.open("Tests/images/color_snakes.png")
class TestImageOpsUsm(PillowTestCase): class TestImageOpsUsm(PillowTestCase):
@ -16,7 +17,7 @@ class TestImageOpsUsm(PillowTestCase):
self.assertEqual(i.size, (128, 128)) self.assertEqual(i.size, (128, 128))
# i.save("blur.bmp") # 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.mode, "RGB")
self.assertEqual(i.size, (128, 128)) self.assertEqual(i.size, (128, 128))
# i.save("usm.bmp") # i.save("usm.bmp")
@ -33,7 +34,7 @@ class TestImageOpsUsm(PillowTestCase):
self.assertEqual(i.mode, "RGB") self.assertEqual(i.mode, "RGB")
self.assertEqual(i.size, (128, 128)) self.assertEqual(i.size, (128, 128))
def test_usm(self): def test_usm_formats(self):
usm = ImageOps.unsharp_mask usm = ImageOps.unsharp_mask
self.assertRaises(ValueError, lambda: usm(im.convert("1"))) self.assertRaises(ValueError, lambda: usm(im.convert("1")))
@ -45,7 +46,7 @@ class TestImageOpsUsm(PillowTestCase):
usm(im.convert("CMYK")) usm(im.convert("CMYK"))
self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr")))
def test_blur(self): def test_blur_formats(self):
blur = ImageOps.gaussian_blur blur = ImageOps.gaussian_blur
self.assertRaises(ValueError, lambda: blur(im.convert("1"))) self.assertRaises(ValueError, lambda: blur(im.convert("1")))
@ -57,6 +58,33 @@ class TestImageOpsUsm(PillowTestCase):
blur(im.convert("CMYK")) blur(im.convert("CMYK"))
self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) 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__': if __name__ == '__main__':
unittest.main() unittest.main()