Pillow/Tests/test_imageops_usm.py

86 lines
2.9 KiB
Python
Raw Normal View History

from PIL import Image, ImageFilter
from .helper import PillowTestCase
im = Image.open("Tests/images/hopper.ppm")
snakes = Image.open("Tests/images/color_snakes.png")
2014-06-10 13:10:47 +04:00
class TestImageOpsUsm(PillowTestCase):
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))
def test_usm_formats(self):
2014-06-10 13:10:47 +04:00
2017-09-14 02:49:05 +03:00
usm = ImageFilter.UnsharpMask
self.assertRaises(ValueError, im.convert("1").filter, usm)
im.convert("L").filter(usm)
self.assertRaises(ValueError, im.convert("I").filter, usm)
self.assertRaises(ValueError, im.convert("F").filter, usm)
im.convert("RGB").filter(usm)
im.convert("RGBA").filter(usm)
im.convert("CMYK").filter(usm)
self.assertRaises(ValueError, im.convert("YCbCr").filter, usm)
2014-06-10 13:10:47 +04:00
def test_blur_formats(self):
2014-06-10 13:10:47 +04:00
2017-09-14 02:49:05 +03:00
blur = ImageFilter.GaussianBlur
self.assertRaises(ValueError, im.convert("1").filter, blur)
2014-06-10 13:10:47 +04:00
blur(im.convert("L"))
2017-09-14 02:49:05 +03:00
self.assertRaises(ValueError, im.convert("I").filter, blur)
self.assertRaises(ValueError, im.convert("F").filter, blur)
im.convert("RGB").filter(blur)
im.convert("RGBA").filter(blur)
im.convert("CMYK").filter(blur)
self.assertRaises(ValueError, im.convert("YCbCr").filter, blur)
2014-06-10 13:10:47 +04:00
def test_usm_accuracy(self):
2019-06-13 18:54:46 +03:00
src = snakes.convert("RGB")
2017-09-14 02:49:05 +03:00
i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0))
# Image should not be changed because it have only 0 and 255 levels.
self.assertEqual(i.tobytes(), src.tobytes())
def test_blur_accuracy(self):
2019-06-13 18:54:46 +03:00
i = snakes.filter(ImageFilter.GaussianBlur(0.4))
# These pixels surrounded with pixels with 255 intensity.
# They must be very close to 255.
2019-06-13 18:54:46 +03: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),
]:
self.assertGreaterEqual(i.im.getpixel((x, y))[c], 250)
# Fuzzy match.
2015-07-03 09:22:56 +03:00
def gp(x, y):
return i.im.getpixel((x, y))
2019-06-13 18:54:46 +03: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)