2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageOps
|
|
|
|
from PIL import ImageFilter
|
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = Image.open("Tests/images/hopper.ppm")
|
2014-10-08 06:49:56 +04:00
|
|
|
snakes = Image.open("Tests/images/color_snakes.png")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
class TestImageOpsUsm(PillowTestCase):
|
|
|
|
|
|
|
|
def test_ops_api(self):
|
|
|
|
|
|
|
|
i = ImageOps.gaussian_blur(im, 2.0)
|
|
|
|
self.assertEqual(i.mode, "RGB")
|
|
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
# i.save("blur.bmp")
|
|
|
|
|
2014-10-08 06:49:56 +04:00
|
|
|
i = ImageOps.unsharp_mask(im, 2.0, 125, 8)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(i.mode, "RGB")
|
|
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
# i.save("usm.bmp")
|
|
|
|
|
|
|
|
def test_filter_api(self):
|
|
|
|
|
2015-12-10 01:23:58 +03:00
|
|
|
test_filter = ImageFilter.GaussianBlur(2.0)
|
|
|
|
i = im.filter(test_filter)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(i.mode, "RGB")
|
|
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
|
2015-12-10 01:23:58 +03:00
|
|
|
test_filter = ImageFilter.UnsharpMask(2.0, 125, 8)
|
|
|
|
i = im.filter(test_filter)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(i.mode, "RGB")
|
|
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
|
2014-10-08 06:49:56 +04:00
|
|
|
def test_usm_formats(self):
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
usm = ImageOps.unsharp_mask
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, usm, im.convert("1"))
|
2014-06-10 13:10:47 +04:00
|
|
|
usm(im.convert("L"))
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, usm, im.convert("I"))
|
|
|
|
self.assertRaises(ValueError, usm, im.convert("F"))
|
2014-06-10 13:10:47 +04:00
|
|
|
usm(im.convert("RGB"))
|
|
|
|
usm(im.convert("RGBA"))
|
|
|
|
usm(im.convert("CMYK"))
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, usm, im.convert("YCbCr"))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-08 06:49:56 +04:00
|
|
|
def test_blur_formats(self):
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
blur = ImageOps.gaussian_blur
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, blur, im.convert("1"))
|
2014-06-10 13:10:47 +04:00
|
|
|
blur(im.convert("L"))
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, blur, im.convert("I"))
|
|
|
|
self.assertRaises(ValueError, blur, im.convert("F"))
|
2014-06-10 13:10:47 +04:00
|
|
|
blur(im.convert("RGB"))
|
|
|
|
blur(im.convert("RGBA"))
|
|
|
|
blur(im.convert("CMYK"))
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(ValueError, blur, im.convert("YCbCr"))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2014-10-08 06:49:56 +04:00
|
|
|
def test_usm_accuracy(self):
|
|
|
|
|
2014-11-22 01:00:04 +03:00
|
|
|
src = snakes.convert('RGB')
|
|
|
|
i = src._new(ImageOps.unsharp_mask(src, 5, 1024, 0))
|
2014-10-08 06:49:56 +04:00
|
|
|
# Image should not be changed because it have only 0 and 255 levels.
|
2014-11-22 01:00:04 +03:00
|
|
|
self.assertEqual(i.tobytes(), src.tobytes())
|
2014-10-08 06:49:56 +04:00
|
|
|
|
|
|
|
def test_blur_accuracy(self):
|
|
|
|
|
2014-10-25 16:53:15 +04:00
|
|
|
i = snakes._new(ImageOps.gaussian_blur(snakes, .4))
|
2014-10-08 06:49:56 +04:00
|
|
|
# These pixels surrounded with pixels with 255 intensity.
|
2014-10-12 14:00:34 +04:00
|
|
|
# They must be very close to 255.
|
2014-10-08 06:49:56 +04:00
|
|
|
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)]:
|
2014-10-12 14:00:34 +04:00
|
|
|
self.assertGreaterEqual(i.im.getpixel((x, y))[c], 250)
|
2014-10-08 06:49:56 +04:00
|
|
|
# Fuzzy match.
|
2015-07-03 09:22:56 +03:00
|
|
|
|
|
|
|
def gp(x, y):
|
|
|
|
return i.im.getpixel((x, y))
|
2014-10-25 16:53:15 +04:00
|
|
|
self.assertTrue(236 <= gp(7, 4)[0] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(7, 5)[2] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(7, 6)[2] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(7, 7)[1] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(8, 4)[0] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(8, 5)[2] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(8, 6)[2] <= 239)
|
|
|
|
self.assertTrue(236 <= gp(8, 7)[1] <= 239)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|