aboutsummaryrefslogtreecommitdiff
path: root/pixelterm
blob: 79c9cd296ba8f7c0eee7cbf8e45bb9cb6cbe51db (plain)
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
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python

import os, sys, argparse, os.path, json
import pixelterm
from multiprocessing import Pool
from PIL import Image, PngImagePlugin

def convert(f):
	img = Image.open(f).convert("RGBA")
	if args.output_dir:
		print(f)
		foo, _, _ = f.rpartition('.png')
		output = os.path.join(args.output_dir, os.path.basename(foo)+'.pony')
		metadata = json.loads(img.info.get('pixelterm-metadata'))
		comment = metadata.get('_comment')
		if comment is not None:
			del metadata['_comment']
			comment = '\n'+comment
		else:
			comment = ''
		metadataarea = '$$$\n' +\
			'\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\
			comment + '\n$$$\n'
		with open(output, 'w') as of:
			of.write(metadataarea)
			of.write(pixelterm.termify_pixels(img))
	else:
		print(pixelterm.termify_pixels(img))

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Render pixel images on 256-color ANSI terminals')
	parser.add_argument('image', type=str, nargs='*')
	parser.add_argument('-d', '--output-dir', type=str, help='Output directory (if not given, output to stdout)')
	args = parser.parse_args()
	p = Pool()
	p.map(convert, args.image)