def test_markov_image_generate_levels(args, kwargs, data, res): scanner = Scanner(lambda x: x) scanner.traversal = [HLines(), VLines()] scanner.levels = 2 scanner.level_scale = [2] markov = MarkovImage(levels=2, scanner=scanner) markov.imgtype.convert = lambda x: [x] if data: markov.data([ ['\x00', '\x01', '\x02', '\x03'], [(Scanner.START, '\x00'), '\x01', (Scanner.START, '\x01'), '\x02', (Scanner.START, '\x02'), '\x03', (Scanner.START, '\x03'), '\x00'] ]) if 'start_image' in kwargs: img = Image.new('P', (1, 1)) img.putpixel((0, 0), 1) kwargs['start_image'] = img if isinstance(res, type): with pytest.raises(res): markov(*args, **kwargs) else: assert list(markov(*args, **kwargs).getdata()) == res
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_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_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_markov_image_generate(test, res): scanner = Scanner(lambda x: x) scanner.traversal = [HLines()] scanner.levels = 1 scanner.level_scale = [] markov = MarkovImage( scanner=scanner, parser=Parser() ) markov.imgtype.convert = lambda x: [x] markov.data([['\x00', '\x01', '\x02', '\x03']]) assert list(markov(*test).getdata()) == res
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_generate(self): scanner = Scanner(lambda x: x) scanner.traversal = [HLines()] scanner.levels = 1 scanner.level_scale = [] m = self.Markov(palette=self.palette, scanner=scanner, parser=Parser()) with self.assertRaises(RuntimeError): m.image(2, 2) m.data([['00', '01', '02', '03']]) data = list(m.image(2, 2).getdata()) self.assertEqual(data, [0, 1, 2, 3]) data = list(m.image(4, 2).getdata()) self.assertEqual(data, [0, 1, 2, 3, 0, 1, 2, 3])
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]]
assert size == [(2, 2), (4, 4), (12, 12), (48, 48)] 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_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]]] @pytest.mark.parametrize( 'test', [(), ((4, 4), 2, 2, Image.NEAREST, [HLines(), VLines()])]) 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_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] ] ] @pytest.mark.parametrize('test', [ (), ((4, 4), 2, 2, 'NEAREST', [HLines(), VLines()]) ]) 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_generate_levels(self): scanner = Scanner(lambda x: x) scanner.traversal = [HLines(), VLines()] scanner.levels = 2 scanner.level_scale = [2] scanner.set_palette = lambda img: img m = self.Markov(levels=2, palette=self.palette, scanner=scanner) with self.assertRaises(RuntimeError): m.image(2, 2) m.data([ ['00', '01', '02', '03',], [(Scanner.START, '0000'), '0101', (Scanner.START, '0001'), '0102', (Scanner.START, '0002'), '0103', (Scanner.START, '0003'), '0100',] ]) data = list(m.image(2, 2, levels=1).getdata()) # pylint: disable=no-member self.assertEqual(data, [0, 1, 2, 3]) data = list(m.image(2, 2, levels=2).getdata()) # pylint: disable=no-member self.assertEqual( data, [ 0, 1, 1, 2, 1, 1, 2, 2, 2, 3, 3, 0, 3, 3, 0, 0 ] ) with self.assertRaises(ValueError): m.image(2, 2, levels=3) with self.assertRaises(ValueError): m.image(2, 2, levels=0) img = Image.new('P', (1, 1)) img.putpalette(self.palette) img.putpixel((0, 0), 1) data = list(m.image( 2, 1, levels=1, start_level=-5, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [0, 1]) data = list(m.image( None, None, levels=1, start_level=0, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1]) data = list(m.image( None, None, levels=1, start_level=2, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1]) data = list(m.image( None, None, levels=2, start_level=0, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1, 2, 2, 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]])