def test_save_load(self): tests = [(), ((4, 4), 0, True, self.palette, 2, 2, Image.NEAREST, [HLines(), VLines()])] for test in tests: scanner = ImageScanner(*test) saved = scanner.save() loaded = Scanner.load(saved) self.assertEqual(scanner, loaded)
def test_input_level_scale(self): img = Image.new(mode='RGB', size=(48, 48)) scan = ImageScanner(palette=self.palette, levels=4, level_scale=[2, 3, 4]) img = scan.input(img) size = [scan.level(img, level).size for level in range(scan.levels)] self.assertEqual(size, [(2, 2), (4, 4), (12, 12), (48, 48)])
def test_image_scanner_input(test, image_test): scan = ImageScanner(resize=test) img = scan.input(image_test) assert img.size <= test assert img.mode == 'P' img = [scan.level(img, level) for level in range(scan.levels)] assert len(img) == 1 img = img[0] assert img.mode == 'P' assert img.size == (1, 1) assert list(img.getdata()) in [[0], [1]]
def test_input(self): tests = [(1, 1), (4, 1), (1, 4)] for test in tests: scan = ImageScanner(palette=self.palette, resize=test) img = scan.input(self.image) self.assertLessEqual(img.size, test) self.assertEqual(img.mode, 'RGB') img = [scan.level(img, level) for level in range(scan.levels)] self.assertEqual(len(img), 1) img = img[0] self.assertEqual(img.mode, 'P') self.assertEqual(img.size, (1, 1)) self.assertIn(list(img.getdata()), [[0], [1]])
def test_image_scanner_init(): scan = ImageScanner() assert scan.levels is not None assert scan.min_size is not None assert isinstance(scan.level_scale, list) assert isinstance(scan.traversal, list) assert len(scan.level_scale) == scan.levels - 1 assert len(scan.traversal) == scan.levels
def test_image_scanner_input_levels(image_test): scan = ImageScanner(resize=(4, 4), levels=3, level_scale=2) img = scan.input(image_test) img = [scan.level(img, level) for level in range(scan.levels)] assert len(img) == 3 assert img[0].mode == 'P' assert img[0].size == (1, 1) assert list(img[0].getdata()) in [[0], [1]] assert img[1].mode == 'P' assert img[1].size == (2, 2) assert list(img[1].getdata()) == [0, 2, 3, 1] assert img[2].mode == 'P' assert img[2].size == (4, 4) assert list( img[2].getdata()) == [0, 0, 2, 2, 0, 0, 2, 2, 3, 3, 1, 1, 3, 3, 1, 1]
def test_image_scanner_scan_levels(image_test): scan = ImageScanner(levels=2, level_scale=2, traversal=[HLines(), VLines()]) assert [list(level) for level in scan(image_test) ] in [[['\x00', scan.END], [(scan.START, '\x00'), '\x00', '\x03', '\x02', '\x01', scan.END]], [['\x01', scan.END], [(scan.START, '\x01'), '\x00', '\x03', '\x02', '\x01', scan.END]]]
def test_scan_levels(self): scan = ImageScanner(palette=self.palette, levels=2, level_scale=2, traversal=[HLines(), VLines()]) self.assertIn([list(level) for level in scan(self.image)], [ [['00', scan.END], [(scan.START, '0000'), '0100', '0103', '0102', '0101', scan.END]], [['01', scan.END], [(scan.START, '0001'), '0100', '0103', '0102', '0101', scan.END]], ])
def test_input_levels(self): scan = ImageScanner(palette=self.palette, levels=2, level_scale=2) img = scan.input(self.image) img = [scan.level(img, level) for level in range(scan.levels)] self.assertEqual(len(img), 2) self.assertEqual(img[0].mode, 'P') self.assertEqual(img[0].size, (1, 1)) self.assertIn(list(img[0].getdata()), [[0], [1]]) self.assertEqual(img[1].mode, 'P') self.assertEqual(img[1].size, (2, 2)) self.assertEqual(list(img[1].getdata()), [0, 2, 3, 1]) scan = ImageScanner(palette=self.palette, levels=3, level_scale=2) with self.assertRaises(ValueError): scan.input(self.image)
def test_image_scanner_input_error(image_test): scan = ImageScanner(levels=3, level_scale=2) with pytest.raises(ValueError): scan.input(image_test)
def test_image_scanner_properties(): scan = ImageScanner() with pytest.raises(ValueError): scan.levels = 0 scan.levels = 3 assert len(scan.traversal) == scan.levels with pytest.raises(ValueError): scan.level_scale = [] with pytest.raises(ValueError): scan.level_scale = [1, -1] scan.level_scale = range(2, 10) assert scan.level_scale == [2, 3] assert scan.min_size == 6 scan.level_scale = 2 assert scan.level_scale == [2, 2] assert scan.min_size == 4 scan.levels = 2 assert len(scan.traversal) == scan.levels assert scan.level_scale == [2] assert scan.min_size == 2 scan.levels = 1 assert len(scan.traversal) == scan.levels assert scan.level_scale == [] assert scan.min_size == 1 traversal = [HLines(), VLines()] scan.traversal = traversal scan.levels = 2 assert scan.traversal is traversal assert scan.level_scale == [2]
def test_image_scanner_save_load(test): scanner = ImageScanner(*test) saved = scanner.save() loaded = Scanner.load(saved) assert isinstance(loaded, ImageScanner) assert scanner == loaded
def test_image_scanner_scan(image_test): scan = ImageScanner(traversal=HLines()) assert [list(level) for level in scan(image_test) ] == [['\x00', '\x02', '\x03', '\x01', scan.END]]
def test_image_scanner_input_level_scale(): img = Image.new(mode='RGB', size=(48, 48)) scan = ImageScanner(levels=4, level_scale=[2, 3, 4]) img = scan.input(img) size = [scan.level(img, level).size for level in range(scan.levels)] assert size == [(2, 2), (4, 4), (12, 12), (48, 48)]
def test_properties(self): scan = ImageScanner() self.assertEqual(scan.palette, default_palette(8, 4, 8)) self.assertIsNotNone(scan.palette_image) self.assertIsNotNone(scan.levels) self.assertIsNotNone(scan.min_size) self.assertIsInstance(scan.level_scale, list) self.assertIsInstance(scan.traversal, list) self.assertEqual(len(scan.level_scale), scan.levels - 1) self.assertEqual(len(scan.traversal), scan.levels) scan.palette = self.palette self.assertIsNotNone(scan.palette) self.assertIsNotNone(scan.palette_image) with self.assertRaises(ValueError): scan.levels = 0 scan.levels = 3 self.assertEqual(len(scan.traversal), scan.levels) with self.assertRaises(ValueError): scan.level_scale = [] with self.assertRaises(ValueError): scan.level_scale = [1, -1] scan.level_scale = range(2, 10) self.assertEqual(scan.level_scale, [2, 3]) self.assertEqual(scan.min_size, 6) scan.level_scale = 2 self.assertEqual(scan.level_scale, [2, 2]) self.assertEqual(scan.min_size, 4) scan.levels = 2 self.assertEqual(len(scan.traversal), scan.levels) self.assertEqual(scan.level_scale, [2]) self.assertEqual(scan.min_size, 2) scan.levels = 1 self.assertEqual(len(scan.traversal), scan.levels) self.assertEqual(scan.level_scale, []) self.assertEqual(scan.min_size, 1) traversal = [HLines(), VLines()] scan.traversal = traversal scan.levels = 2 self.assertIs(scan.traversal, traversal) self.assertEqual(scan.level_scale, [2])
def test_scan(self): scan = ImageScanner(palette=self.palette, traversal=HLines()) self.assertEqual([list(level) for level in scan(self.image)], [['00', '02', '03', '01', scan.END]])