def test_sanity(self): def filter(filter): for mode in ["L", "RGB", "CMYK"]: im = hopper(mode) out = im.filter(filter) self.assertEqual(out.mode, im.mode) self.assertEqual(out.size, im.size) filter(ImageFilter.BLUR) filter(ImageFilter.CONTOUR) filter(ImageFilter.DETAIL) filter(ImageFilter.EDGE_ENHANCE) filter(ImageFilter.EDGE_ENHANCE_MORE) filter(ImageFilter.EMBOSS) filter(ImageFilter.FIND_EDGES) filter(ImageFilter.SMOOTH) filter(ImageFilter.SMOOTH_MORE) filter(ImageFilter.SHARPEN) filter(ImageFilter.MaxFilter) filter(ImageFilter.MedianFilter) filter(ImageFilter.MinFilter) filter(ImageFilter.ModeFilter) filter(ImageFilter.GaussianBlur) filter(ImageFilter.GaussianBlur(5)) filter(ImageFilter.BoxBlur(5)) filter(ImageFilter.UnsharpMask) filter(ImageFilter.UnsharpMask(10)) self.assertRaises(TypeError, filter, "hello")
def test_numpy_sources(self): table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16) with self.assertRaisesRegex(ValueError, "should have either channels"): lut = ImageFilter.Color3DLUT((5, 6, 7), table) table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertIsInstance(lut.table, numpy.ndarray) self.assertEqual(lut.table.dtype, table.dtype) self.assertEqual(lut.table.shape, (table.size, )) table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertEqual(lut.table.shape, (table.size, )) table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertEqual(lut.table.shape, (table.size, )) # Check application Image.new('RGB', (10, 10), 0).filter(lut) # Check copy table[0] = 33 self.assertEqual(lut.table[0], 1) # Check not copy table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False) table[0] = 33 self.assertEqual(lut.table[0], 33)
def test_wrong_args(self): with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT("small", [1]) with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT((11, 11), [1]) with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 1), [1]) with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 66), [1]) with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1]) with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2) with self.assertRaisesRegex(ValueError, "should have a length of 4"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4) with self.assertRaisesRegex(ValueError, "should have a length of 3"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8) with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8, channels=2)
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_repr(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) self.assertEqual(repr(lut), "<Color3DLUT from list size=2x2x2 channels=3>") lut = ImageFilter.Color3DLUT((3, 4, 5), array('f', [0, 0, 0, 0] * (3 * 4 * 5)), channels=4, target_mode='YCbCr', _copy_table=False) self.assertEqual( repr(lut), "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>")
def test_convert_table(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.name, "Color 3D LUT") lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)]) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.table, list(range(24))) lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, channels=4)
def test_consistency_3x3(self): source = Image.open("Tests/images/hopper.bmp") reference = Image.open("Tests/images/hopper_emboss.bmp") kernel = ImageFilter.Kernel((3, 3), (-1, -1, 0, -1, 0, 1, 0, 1, 1), .3) source = source.split() * 2 reference = reference.split() * 2 for mode in ['L', 'LA', 'RGB', 'CMYK']: self.assert_image_equal( Image.merge(mode, source[:len(mode)]).filter(kernel), Image.merge(mode, reference[:len(mode)]), )
def test_blur(self): # test case from irc, how to do blur on b/w image # and save to compressed tif. from PIL2 import ImageFilter out = self.tempfile('temp.tif') im = Image.open('Tests/images/pport_g4.tif') im = im.convert('L') im = im.filter(ImageFilter.GaussianBlur(4)) im.save(out, compression='tiff_adobe_deflate') im2 = Image.open(out) im2.load() self.assert_image_equal(im, im2)
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)
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_imageops_box_blur(self): i = sample.filter(ImageFilter.BoxBlur(1)) self.assertEqual(i.mode, sample.mode) self.assertEqual(i.size, sample.size) self.assertIsInstance(i, Image.Image)
def test_kernel_not_enough_coefficients(self): self.assertRaises(ValueError, lambda: ImageFilter.Kernel((3, 3), (0, 0)))
def test_builtinfilter_p(self): builtinFilter = ImageFilter.BuiltinFilter() self.assertRaises(ValueError, builtinFilter.filter, hopper("P"))
def test_rankfilter_properties(self): rankfilter = ImageFilter.RankFilter(1, 2) self.assertEqual(rankfilter.size, 1) self.assertEqual(rankfilter.rank, 2)