def test_on_triangle(self): ascii = convert_to_ascii.convert( 'tests/img/primitives/triangle.jpg', (20, 10), 'palettes/UbuntuMono.plt', False) ascii = cut_top_bottom(ascii)[0] previous = '' for line in ascii: l_bs = line.index(' ') r_bs = line.rfind(' ') self.assertNotEqual(line[l_bs - 1], ' ') self.assertNotEqual(line[r_bs + 1], ' ') p_bs = previous.count(' ') c_bs = line.count(' ') previous = line self.assertLessEqual(p_bs, c_bs)
def test_on_square(self): ascii = convert_to_ascii.convert( 'tests/img/primitives/square.jpg', (20, 10), 'palettes/UbuntuMono.plt', False) ascii, top, bottom = cut_top_bottom(ascii) left = ascii[0].index(' ') right = ascii[0].rfind(' ') for line in ascii: l_bs = line[left - 1] r_bs = line[right + 1] self.assertEqual(l_bs, 'N') self.assertEqual(r_bs, 'N') self.assertAlmostEqual(2 * (bottom - top), right - left, delta=1) ascii = [''.join(line[left:right]) for line in ascii] self.assertEqual(''.join(ascii).strip(), '')
def zoom(self, ch, step=0.1): kf = 1 - step if ch == ord('-') else 1 / (1 - step) self.screen.clear() self.screen.addstr(0, 0, 'loading...') self.screen.refresh() content = convert_to_ascii.convert( args.image.name, (int((self.max_x - 1) * kf), int(self.max_y * kf)), args.palette, args.inverse) self.max_y = len(content) self.max_x = len(content[0]) + 1 self.pad.resize(self.max_y, self.max_x) for i in range(len(content)): self.pad.addstr(i, 0, content[i]) self.current_y = 0 self.current_x = 0 self.refresh_pad()
def test_on_circle(self): ascii = convert_to_ascii.convert( 'tests/img/primitives/circle.jpg', (20, 10), 'palettes/UbuntuMono.plt', False) ascii = cut_top_bottom(ascii)[0] previous = '' half = False p_diff = 0 for i in range(len(ascii)): p_bs = previous.count(' ') c_bs = ascii[i].count(' ') c_diff = abs(p_bs - c_bs) if not half: self.assertLessEqual(p_bs, c_bs) else: self.assertGreaterEqual(p_bs, c_bs) if p_bs == c_bs and previous: half = True self.assertTrue(p_diff != c_diff or p_diff == 0) p_diff = c_diff previous = ascii[i]
self.pad.keypad(False) curses.endwin() def get_args(): arg_parser = argparse.ArgumentParser( description='Convert image to ASCII-art') arg_parser.add_argument('image', type=argparse.FileType(), help='image') arg_parser.add_argument('-s', '--size', type=int, nargs=2, action='store', metavar=('width', 'height'), help='output image size (in characters)') arg_parser.add_argument('-p', '--palette', type=str, help='palette file', default='palettes/UbuntuMono.plt') arg_parser.add_argument('-i', '--inverse', action='store_true', help='invert palette (default: white characters on black)') arg_parser.add_argument('--interactive', action='store_true', help='live view of the result') return arg_parser.parse_args() if __name__ == '__main__': args = get_args() ascii = convert_to_ascii.convert(args.image.name, args.size, args.palette, args.inverse) if args.interactive: Display(ascii).show() else: for line in ascii: print(line)