"""Downsample a screenshot to coarse ASCII so art can be sanity-checked in a terminal. Usage: python asciipic.py shots/3_play_2x.png""" import sys import pygame NAMES = { "P": (255, 130, 195), "G": (255, 214, 92), "M": (110, 240, 200), "W": (240, 240, 250), "B": (120, 205, 255), "f": (64, 50, 140), "b": (22, 15, 56), "d": (14, 9, 42), "V": (200, 120, 255), "K": (255, 230, 225), "r": (255, 90, 130), "c": (90, 70, 160), } def main(): path = sys.argv[1] surf = pygame.image.load(path) w, h = surf.get_size() cw, ch = 6, 7 print("image", w, h) keys = list(NAMES) for row in range(0, h, ch): line = [] for col in range(0, w, cw): # average color in cell acc = [0, 0, 0] n = 0 for y in range(row, min(row + ch, h), 2): for x in range(col, min(col + cw, w), 2): px = surf.get_at((x, y)) acc[0] += px[0]; acc[1] += px[1]; acc[2] += px[2] n += 1 if n == 0: line.append("?") continue avg = tuple(a / n for a in acc) # nearest named color, weighted best, bd = "?", 1e9 for k, c in NAMES.items(): d = (avg[0] - c[0]) ** 2 * 1.2 + (avg[1] - c[1]) ** 2 + (avg[2] - c[2]) ** 2 if d < bd: bd, best = d, k # dark cells just show a dot of their tone line.append(best) print("".join(line)) if __name__ == "__main__": main()