ascii.py (1206B)
1 #!/usr/bin/env python3 2 # logo.png as a coloured text logo for fastfetch ($1 green, $2 red, $3 blue) 3 # usage: ascii.py logo.png COLS > logo.txt 4 import sys 5 from cutout import read_png 6 7 w, h, px = read_png(sys.argv[1]) 8 cols = int(sys.argv[2]) 9 rows = cols // 2 10 out = [] 11 for r in range(rows): 12 line, cur = '', None 13 for c in range(cols): 14 votes = {} 15 for y in range(r * h // rows, (r + 1) * h // rows, 3): 16 for x in range(c * w // cols, (c + 1) * w // cols, 3): 17 R, G, B, A = px[y * w + x] 18 if A < 128: 19 k = None 20 elif G > R and G > B: 21 k = '1' 22 elif R > G and R > B: 23 k = '2' 24 else: 25 k = '3' 26 votes[k] = votes.get(k, 0) + 1 27 k = max(votes, key=votes.get) 28 if k is None or votes[k] < sum(votes.values()) * 0.4: 29 line += ' ' 30 continue 31 if k != cur: 32 line += '$' + k 33 cur = k 34 line += '#' 35 out.append(line.rstrip()) 36 while out and not out[-1].replace('$1', '').replace('$2', '').replace('$3', '').strip(): 37 out.pop() 38 print('\n'.join(out))