-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_im.py
More file actions
39 lines (28 loc) · 1.34 KB
/
print_im.py
File metadata and controls
39 lines (28 loc) · 1.34 KB
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
import collections
import itertools
import operator
import re
import sys
# convert -thumbnail 80x80 img.jpeg txt:- > img.txt
pattern = re.compile(r"^(?P<col>\d+),(?P<line>\d+): \((?P<red>-?\d+)(\.\d+)?,(?P<green>-?\d+)(\.\d+)?,(?P<blue>-?\d+)(\.\d+)?,(?P<alpha>\d+)")
Pixel = collections.namedtuple("Pixel", ("col", "line", "red", "green", "blue", "alpha"))
def printstackedpixels(top, bottom=Pixel(0, 0, 0, 0, 0, 255)):
sys.stdout.write("\033[38;2;{top.red};{top.green};{top.blue}m"
"\033[48;2;{bottom.red};{bottom.green};{bottom.blue}m\u2580".format(top=top, bottom=bottom))
def parseline(line):
pixel = pattern.match(line).groupdict()
pixel["line"] = int(pixel["line"])
for color in ["red", "green", "blue"]:
pixel[color] = int(int(pixel[color]) / 65535 * 255)
return Pixel(**pixel)
def linegroup(pixel):
return pixel.line - (pixel.line % 2)
if __name__ == "__main__":
with open(sys.argv[1]) as f:
lines = (parseline(line) for line in f
if not line.startswith("#"))
for _, pixelgroup in itertools.groupby(lines, linegroup):
for _, pixels in itertools.groupby(sorted(pixelgroup, key=operator.attrgetter("col")),
key=operator.attrgetter("col")):
printstackedpixels(*pixels)
print()