def test_blur_3(self): inpfile = os.path.join(TEST_DIRECTORY, 'test_images', '%s.png' % 'centered_pixel') expected_3 = lab.Image(11, 11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) expected_5 = lab.Image(11,11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) result_3 = lab.Image.load(inpfile).blurred(3) result_5 = lab.Image.load(inpfile).blurred(5) self.assertEqual(expected_3,result_3) self.assertEqual(expected_5,result_5)
def test_inverted_2(self): # REPLACE THIS from your test case from section 3.1 im = lab.Image(4, 1, [24, 93, 140, 197]) result = im.inverted() expected = lab.Image(4, 1, [231, 162, 115, 58]) print(result.pixels) self.assertEqual(result, expected)
def test_blur(self): for kernsize in (1, 3, 7): for fname in ('mushroom', 'twocats', 'chess'): with self.subTest(k=kernsize, f=fname): inpfile = os.path.join(TEST_DIRECTORY, 'test_images', '%s.png' % fname) expfile = os.path.join( TEST_DIRECTORY, 'test_results', '%s_blur_%02d.png' % (fname, kernsize)) result = lab.Image.load(inpfile).blurred(kernsize) expected = lab.Image.load(expfile) self.assertEqual(result, expected) ## PERSONAL TEST CASES: for kernsize in (3, 7): input_image = lab.Image(6, 5, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) result = input_image.blurred(kernsize) expected = lab.Image(6, 5, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(result, expected) for kernsize in (3, 5): input_image = lab.Image.load("test_images/centered_pixel.png") results = input_image.blurred(kernsize) if kernsize == 3: expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) elif kernsize == 5: expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(results, expected)
def test_correlation_1(self): expected = lab.Image.load('test_images/centered_pixel.png') kernel = lab.Image(3, 3, [0, 0, 0, 0, 1, 0, 0, 0, 0]) result = expected.filtered(kernel) self.assertEqual(result, expected)
def test_blur2(self): im = lab.Image(6, 5, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) for kernsize in (3, 5): result = im.blurred(kernsize) self.assertEqual(result, im)
def test_correlation_3(self): im = lab.Image.load('test_images/centered_pixel.png') kernel = lab.Image(3, 3, [0, 0.2, 0, 0.2, 0.2, 0.2, 0, 0.2, 0]) result = im.filtered(kernel) expected = lab.Image(11, 11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 51, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.assertEqual(result, expected)
def test_blur_2(self): im = lab.Image(6,5, [0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0]) for kernsize in (3,7): im_blurred = im.blurred(kernsize) self.assertEqual(im_blurred, im)
def test_get_unbounded_pixel(self): im = lab.Image(3, 3, [21, 57, 167, 215, 32, 209, 2, 98, 182]) data = (((-2, -1), 21), ((1, -2), 57), ((4, -3), 167), ((-1, 1), 215), ((1, 1), 32), ((5, 1), 209), ((-3, 3), 2), ((1, 4), 98), ((5, 3), 182)) for coordinates, value in data: with self.subTest(c=coordinates): x, y = coordinates result = im.get_unbounded_pixel(x, y) self.assertEqual(result, value)
def test_edges(self): for fname in ('mushroom', 'twocats', 'chess'): with self.subTest(f=fname): inpfile = os.path.join(TEST_DIRECTORY, 'test_images', '%s.png' % fname) expfile = os.path.join(TEST_DIRECTORY, 'test_results', '%s_edges.png' % fname) input_img = lab.Image.load(inpfile) input_img_copy = lab.Image(input_img.width, input_img.height, input_img.pixels) result = input_img.edges() expected = lab.Image.load(expfile) self.assertEqual(input_img, input_img_copy, "Be careful not to modify the original image!") self.assertEqual(result, expected)
def test_load(self): result = lab.Image.load('test_images/centered_pixel.png') expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(result, expected)
def test_centered_1(self): im = lab.Image.load('test_images/centered_pixel.png') expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) result = im.blurred(1) self.assertEqual(result, expected)
def test_edges_1(self): im = lab.Image.load('test_images/centered_pixel.png') result = im.edges() expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(result, expected)
def test_sharpened(self): for kernsize in (1, 3, 9): for fname in ('mushroom', 'twocats', 'chess'): with self.subTest(k=kernsize, f=fname): inpfile = os.path.join(TEST_DIRECTORY, 'test_images', '%s.png' % fname) expfile = os.path.join(TEST_DIRECTORY, 'test_results', '%s_sharp_%02d.png' % (fname, kernsize)) input_img = lab.Image.load(inpfile) input_img_copy = lab.Image(input_img.width, input_img.height, input_img.pixels) result = input_img.sharpened(kernsize) expected = lab.Image.load(expfile) self.assertEqual(input_img, input_img_copy, "Be careful not to modify the original image!") self.assertEqual(result, expected)
def test_correlate_1(self): kernel = [[0, 0, 0], [0, 1, 0], [0, 0, 0]] im = lab.Image.load('test_images/centered_pixel.png') result = im.correlate(kernel) expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(result, expected)
def test_blur_2(self): im = lab.Image.load('test_images/centered_pixel.png') result1 = im.blurred(3) result2 = im.blurred(5) expected1 = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) expected2 = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) self.assertEqual(result1, expected1) self.assertEqual(result2, expected2)
def test_centered_3(self): im = lab.Image.load('test_images/centered_pixel.png') expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) result = im.blurred(3) print(result.pixels) self.assertEqual(result, expected)
def test_corr_2(self): im = lab.Image(5, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]) result = im.correlation([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) expected = lab.Image(5, 5, [1, 1, 1, 2, 3, 6, 6, 6, 7, 8, 11, 11, 11, 12, 13, 16, 16, 16, 17, 18, 21, 21, 21, 22, 23]) self.assertEqual(result, expected)
def test_correlate_3(self): im = lab.Image.load('test_images/centered_pixel.png') kernel = [[0, 0.2, 0], [0.2, 0.2, 0.2], [0, 0.2, 0]] result = im.correlate(kernel) expected = expected = lab.Image(11, 11, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 51, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) #images_differ(result, expected) self.assertEqual(result, expected)
def test_edges_2(self): inpfile = os.path.join(TEST_DIRECTORY, 'test_images', '%s.png' %'centered_pixel') result = lab.Image.load(inpfile).edges() expected = lab.Image(11, 11, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) self.assertEqual(result, expected)
def test_invert_1(self): im = lab.Image.load('test_images/centered_pixel.png') result = im.inverted() expected = lab.Image(11, 11, [ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 ]) self.assertEqual(result, expected)
def test_correlate(self): identity = ((0, 0, 0), (0, 1, 0), (0, 0, 0)) translation = ((0, 0, 0, 0, 0), (0, 0, 0, 0, 0), (1, 0, 0, 0, 0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0)) average = ((0.0, 0.2, 0.0), (0.2, 0.2, 0.2), (0.0, 0.2, 0.0)) kernels = {"identity": identity, "translation": translation, "average": average} img = lab.Image.load("test_images/centered_pixel.png") img_copy = lab.Image(img.width, img.height, img.pixels) for kernel_name in ("identity", "translation", "average"): with self.subTest(k=kernel_name): expfile = os.path.join(TEST_DIRECTORY, "test_results", f"{kernel_name}_pixel.png") result = img.correlate(kernels[kernel_name]) expected = lab.Image.load(expfile) self.assertEqual(img, img_copy, "Be careful not to modify theo original image!") self.assertEqual(result, expected)
def test_inverted_2(self): # REPLACE THIS from your test case from section 3.1 img = lab.Image(1,4, [3, 95, 141, 213]) result = img.inverted() expected = lab.Image(1,4,[252,160,114,42]) self.assertTrue(result, expected)
def test_invert_2(self): im = lab.Image(1, 4, [30, 81, 141, 220]) result = im.inverted() expected = lab.Image(1, 4, [225, 174, 114, 35]) self.assertEqual(result, expected)
def test_blur_1(self): im = lab.Image(6, 5, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]) result1 = im.blurred(3) result2 = im.blurred(5) expected = lab.Image(6, 5, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]) self.assertEqual(result1, expected) self.assertEqual(result2, expected)
def test_invert_2(self): # REPLACE THIS from your test case from section 3.1 im = lab.Image(4, 1, [20, 85, 144, 197]) result = im.inverted() expected = lab.Image(4, 1, [235, 170, 111, 58]) self.assertEqual(result, expected)
def test_inverted_2(self): im = lab.Image(4, 1, [18, 83, 151, 219]) result = im.inverted() expected = lab.Image(4, 1, [237, 172, 104, 36]) self.assertEqual(result, expected)
def test_trivial_7(self): im = lab.Image(6, 5, [0] * 30) expected = lab.Image(6, 5, [0] * 30) result = im.blurred(5) self.assertEqual(result, expected)
def test_inverted_3(self): im = lab.Image(2, 4, [255, 0, 1, 254, 255, 0, 1, 254]) result = im.inverted() expected = lab.Image(2, 4, [0, 255, 254, 1, 0, 255, 254, 1]) self.assertEqual(result, expected)
def test_inverted_2(self): # REPLACE THIS from your test case from section 3.1 im = lab.Image(1, 4, [18, 83, 151, 219]) result = im.inverted() expected = lab.Image(1, 4, [237, 172, 104, 36]) self.assertEqual(result, expected)
def test_inverted_2(self): im = lab.Image(4,1,[6, 71, 154, 212]) result = im.inverted() expected = lab.Image(4,1,[249,184,101,43]) self.assertEqual(result, expected)