#!/usr/bin/env python import os, sys, argparse, os.path #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): #print('\\e'+seq[1:]) codes = list(map(int, seq.lstrip('\x1b[').rstrip('m').split(';'))) fg, bg = None, None i = 0 while i 1 and args.output: parser.print_help() print('You probably do not want to overwrite the given output file {} times.'.format(len(args.input))) sys.exit(1) for f in args.input: if len(args.input) > 1: print(f.name) img, metadata = unpixelterm(f.read()) if args.verbose: print('Metadata:') pnginfo = PngImagePlugin.PngInfo() for k, v in metadata.items(): if args.verbose: print('{:15}: {}'.format(k, '/'.join(v))) pnginfo.add_text(k, '/'.join(v)) output = args.output or f.name.rstrip('.pony')+'.png' if args.output_dir: output = os.path.join(args.output_dir, os.path.basename(output)) img.save(output, 'PNG', pnginfo=pnginfo)