#!/usr/bin/env python import os, sys, os.path from collections import defaultdict #NOTE: This script uses pygments for X256->RGB conversion since pygments is #readily available. If you do not like pygments (e.g. because it is large), #you could patch in something like https://github.com/magarcia/python-x256 #(but don't forget to send me a pull request ;) from pygments.formatters import terminal256 from PIL import Image, PngImagePlugin try: import re2 as re except: import re formatter = terminal256.Terminal256Formatter() #HACK this adds two missing entries to pygment's color table formatter.xterm_colors.append((0xe4, 0xe4, 0xe4)) formatter.xterm_colors.append((0xee, 0xee, 0xee)) def parse_escape_sequence(seq): codes = list(map(int, seq[2:-1].split(';'))) fg, bg = None, None i = 0 while i w: #Fuck special cases. w = bw img = Image.new('RGBA', (w, h)) fg, bg = (0,0,0,0), (0,0,0,0) x, y = 0, 0 for line in lines: for escapeseq, specialstr, char in re.findall(r'(\x1b\[[0-9;]+m)|(\$[^$]+\$)|(.)', line, re.DOTALL): if escapeseq: nfg, nbg = parse_escape_sequence(escapeseq) fg, bg = nfg or fg, nbg or bg elif specialstr: if specialstr == '$\\$': img.putpixel((x, y), (255, 0, 0, 127)) img.putpixel((x, y+1), (255, 0, 0, 127)) x += 1 elif specialstr == '$/$': img.putpixel((x, y), (0, 0, 255, 127)) img.putpixel((x, y+1), (0, 0, 255, 127)) x += 1 else: #(should be a) balloon for i in range(x, x+bw): img.putpixel((i, y), (0, 255, 0, 127)) img.putpixel((i, y+1), (0, 255, 0, 127)) x += bw elif char: #Da magicks: ▀█▄ c = {' ': (bg, bg), '█': (fg, fg), '▀': (fg, bg), '▄': (bg, fg)}[char] img.putpixel((x, y), c[0]) img.putpixel((x, y+1), c[1]) x += 1 x, y = 0, y+2 return img, metadata