-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimv.py
More file actions
73 lines (62 loc) · 2.27 KB
/
timv.py
File metadata and controls
73 lines (62 loc) · 2.27 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
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
67
68
69
70
71
72
73
'''
timv script
timv is a Terminal Image Viewer
\033[48;2;r;g;b -> background
\033[38;2;r;g;b -> foreground
Unicode Block Element:
https://www.fileformat.info/info/unicode/block/block_elements/list.htm
'''
from PIL import Image
import os
import sys, getopt
def imgToAnsi(img_file, label=False, percent=100):
im = Image.open(img_file, mode='r').convert('RGB')
if percent != 100:
im = im.resize((int(im.size[0]*(percent/100)), int(im.size[1]*(percent/100))))
w,h = im.size
rows,cols = int(h/16), int(w/8)
for r in range(0, rows):
for c in range (0, cols):
box = (c*8, r*16, (c*8)+8, (r*16)+16)
region = im.crop(box)
# split in two part the region (top, bottom)
r_a = region.crop((0,0,8,8))
r_b = region.crop((0,8,8,16))
pixels_a, pixels_b = r_a.getcolors(), r_b.getcolors()
# t -> (count, pixel)
s_pixels_a = sorted(pixels_a, key=lambda t: t[0], reverse=True)
s_pixels_b = sorted(pixels_b, key=lambda t: t[0], reverse=True)
# get the most frequent color
red_a, green_a, blue_a = s_pixels_a[0][1]
red_b, green_b, blue_b = s_pixels_b[0][1]
#print(red, green, blue)
print('\033[48;2;%i;%i;%i;38;2;%i;%i;%im\u2584\033[0m' % (red_a, green_a, blue_a, red_b, green_b, blue_b), end='')
print(end='\n')
#im.show()
if label:
base = os.path.basename(img_file)
print('\033[48;2;0;0;0m%s\033[0m' % base.center(cols))
def printHelp():
print('timv.py [option] image')
print(' -l, --label\t Display file name')
print(' -p, --percent\t Output percentage')
print('Ex. timv.py -l -p 50 image.jpg')
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'hlp:', ['help','label','percent='])
except getopt.GetoptError:
printHelp()
sys.exit()
#img_f = args
label = False
percent = 100
for opt, arg in opts:
if opt in ('-h', '--help') or len(args) < 1:
printHelp()
sys.exit()
elif opt in ('-l', '--label'):
label = True
elif opt in ('-p', '--percent'):
percent = int(arg)
for f in args:
imgToAnsi(f, label=label, percent=percent)