-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuimage.py
executable file
·67 lines (53 loc) · 1.54 KB
/
cuimage.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
60
61
62
63
64
65
66
#!/usr/bin/python
from PIL import Image
import math
import argparse
import subprocess
#=== define ===
COLOR=['0;40','5;40','0;41','5;41','0;42','5;42','0;43','5;43','0;44','5;44','0;45','5;45','0;46','5;46','0;47','5;47']
COLOR_RGB=[[0,0,0],[85,85,85],[187,0,0],[255,85,85],[0,187,0],[85,255,85],[187,187,0],[255,255,85],[0,0,187],[85,85,255],[187,0,187],[255,85,255],[0,187,187],[85,255,255],[187,187,187],[255,255,255]]
#==============
#=== argparse ===
parser = argparse.ArgumentParser()
parser.add_argument('img',help='image file')
parser.add_argument('-s','--save',help='save file',default="")
parser.add_argument('-l','--line',help='output line',type=int,default=32)
args = parser.parse_args()
#================
SYMBOL=' '
LINE=args.line
img=Image.open(args.img)
width=img.size[0]
height=img.size[1]
if height>LINE:
img.thumbnail((width*LINE/height,LINE))
img=img.convert('RGB')
width=img.size[0]
height=img.size[1]
code='echo -e '
for h in xrange(height):
prev_i=-1
for w in xrange(width):
#print w,h
color_i=0
r,g,b=img.getpixel((w,h))
#choice a color
min_d=float("inf")
for i,RGB in enumerate(COLOR_RGB):
d=math.sqrt((RGB[0]-r)**2 + (RGB[1]-g)**2 + (RGB[2]-b)**2)
if math.sqrt((RGB[0]-r)**2 + (RGB[1]-g)**2 + (RGB[2]-b)**2)<min_d:
min_d=d
color_i=i
if prev_i!=color_i:
code += '\\e['+COLOR[color_i]+'m'+SYMBOL
else:
code += SYMBOL
prev_i=color_i
code += '\\e[m\\n'
code = code[:-2]
if args.save!="":
f=file(args.save,'w')
f.write('#!/usr/bin/zsh\n')
f.write(code)
else:
subprocess.call(code.split(' '))