aboutsummaryrefslogtreecommitdiff
path: root/pixelterm.py
blob: db6227e63e8052d252beaa463437ac322462eb9e (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python

import os, sys, argparse
from pygments.formatters import terminal256
from PIL import Image, PngImagePlugin

def termify_pixels(img):
	sx, sy = img.size
	out = ''
	formatter = terminal256.Terminal256Formatter()
	fg,bg = None,None
	def bgescape(color):
		nonlocal bg
		if bg == color:
			return ''
		bg=color
		r,g,b,a = color
		if color == (0,0,0,0):
			return terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string()
		return terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string()
	def fgescape(color):
		nonlocal fg
		if fg == color:
			return ''
		fg=color
		r,g,b,_ = color
		return terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string()
	#NOTE: This ignores the last line if there is an odd number of lines.
	for y in range(0, sy-1, 2):
		for x in range(sx):
			coltop = img.getpixel((x, y))
			colbot = img.getpixel((x, y+1))
			if coltop[3] != 255:
				coltop = (0,0,0,0)
			if colbot[3] != 255:
				colbot = (0,0,0,0)
			c  = '▀'
			te = fgescape
			be = bgescape
			#Da magicks: ▀█▄
			if coltop == (0,0,0,0):
				c,te,be = '▄',be,te
			if colbot == coltop:
				c = ' '
			out += te(coltop)
			out += be(colbot)
			out += c
		out += '\n'
	return out

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Render pixel images on 256-color ANSI terminals')
	parser.add_argument('image', type=str)
	args = parser.parse_args()
	img = Image.open(args.image).convert("RGBA")
	print(termify_pixels(img))