-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2ansi2.py
executable file
·59 lines (49 loc) · 1.41 KB
/
png2ansi2.py
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
57
58
59
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# PNG to ANSI text
#
# Fredrik Hultin 2009
# http://nurd.se/~noname
import gd, sys
if len(sys.argv) < 2 or sys.argv[-1] == "-d":
print " PNG2ANSI"
print " Usage:"
print " %s [-d] <file>" % sys.argv[0]
print " Options:"
print " -d double horizontal \"pixels\""
sys.exit()
hp = 1 + (sys.argv[1] == "-d")
image = gd.image(sys.argv[-1])
pal = gd.image((1, 1))
w, h = image.size()
#pal.colorAllocate((0, 0, 0)) # black
#pal.colorAllocate((128, 0, 0)) # red
#pal.colorAllocate((0, 128, 0)) # green
#pal.colorAllocate((128, 128, 0)) # yellow
#pal.colorAllocate((0, 0, 128)) # blue
#pal.colorAllocate((128, 0, 128)) # magenta
#pal.colorAllocate((0, 128, 128)) # cyan
#pal.colorAllocate((128, 128, 128)) # white
pal.colorAllocate((0, 0, 0)) # black
pal.colorAllocate((256, 0, 0)) # red
pal.colorAllocate((0, 256, 0)) # green
pal.colorAllocate((256, 256, 0)) # yellow
pal.colorAllocate((0, 0, 128)) # blue
pal.colorAllocate((256, 0, 256)) # magenta
pal.colorAllocate((0, 256, 256)) # cyan
pal.colorAllocate((256, 256, 256)) # white
for y in range(h):
for x in range(w):
color = image.colorComponents(image.getPixel((x, y)))
index = pal.colorClosest(color)
out = "%d" % (index)
if index >= 8:
if index == 8:
out = "2"
elif index == 9:
out = "6"
else:
out = "%d" % (index)
for i in range(hp):
sys.stdout.write(out)
sys.stdout.write('\n')