mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
7da17ad41e
The previous test configuration made it difficult to run a single test with the pytest CLI. There were two major issues: - The Tests directory was not a package. It now includes a __init__.py file and imports from other tests modules are done with relative imports. - setup.cfg always specified the Tests directory. So even if a specific test were specified as a CLI arg, this configuration would also always include all tests. This configuration has been removed to allow specifying a single test on the command line. Contributors can now run specific tests with a single command such as: $ tox -e py37 -- Tests/test_file_pdf.py::TestFilePdf.test_rgb This makes it easy and faster to iterate on a single test failure and is very familiar to those that have previously used tox and pytest. When running tox or pytest with no arguments, they still discover and runs all tests in the Tests directory.
106 lines
3.8 KiB
Python
106 lines
3.8 KiB
Python
from .helper import unittest, PillowTestCase
|
|
|
|
from PIL import Image
|
|
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):
|
|
|
|
def test_ops_api(self):
|
|
|
|
i = self.assert_warning(DeprecationWarning,
|
|
ImageOps.gaussian_blur, im, 2.0)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
i = self.assert_warning(DeprecationWarning, ImageOps.box_blur, im, 1)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
i = self.assert_warning(DeprecationWarning, ImageOps.gblur, im, 2.0)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
i = self.assert_warning(DeprecationWarning,
|
|
ImageOps.unsharp_mask, im, 2.0, 125, 8)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
i = self.assert_warning(DeprecationWarning,
|
|
ImageOps.usm, im, 2.0, 125, 8)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
def test_filter_api(self):
|
|
|
|
test_filter = ImageFilter.GaussianBlur(2.0)
|
|
i = im.filter(test_filter)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
test_filter = ImageFilter.UnsharpMask(2.0, 125, 8)
|
|
i = im.filter(test_filter)
|
|
self.assertEqual(i.mode, "RGB")
|
|
self.assertEqual(i.size, (128, 128))
|
|
|
|
def test_usm_formats(self):
|
|
|
|
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)
|
|
|
|
def test_blur_formats(self):
|
|
|
|
blur = ImageFilter.GaussianBlur
|
|
self.assertRaises(ValueError, im.convert("1").filter, blur)
|
|
blur(im.convert("L"))
|
|
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)
|
|
|
|
def test_usm_accuracy(self):
|
|
|
|
src = snakes.convert('RGB')
|
|
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):
|
|
|
|
i = snakes.filter(ImageFilter.GaussianBlur(.4))
|
|
# These pixels surrounded with pixels with 255 intensity.
|
|
# They must be very close to 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.assertGreaterEqual(i.im.getpixel((x, y))[c], 250)
|
|
# Fuzzy match.
|
|
|
|
def gp(x, y):
|
|
return i.im.getpixel((x, y))
|
|
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)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|