diff options
author | jaseg <s@jaseg.de> | 2013-07-30 13:33:07 +0200 |
---|---|---|
committer | jaseg <s@jaseg.de> | 2013-07-30 13:43:23 +0200 |
commit | 5e763123f87422e00810bacc0346936a2ae52d3d (patch) | |
tree | db51c86622847e6e46dd1687cba90a71b7d72cc7 /unpixelterm.py | |
parent | 76579920ef33af869ed9c643234fdb4a3dfbd7a1 (diff) | |
download | pixelterm-5e763123f87422e00810bacc0346936a2ae52d3d.tar.gz pixelterm-5e763123f87422e00810bacc0346936a2ae52d3d.tar.bz2 pixelterm-5e763123f87422e00810bacc0346936a2ae52d3d.zip |
Removed standard colors 0-15 for portability. Removed pygments dep. Fixed setup.py
Diffstat (limited to 'unpixelterm.py')
-rw-r--r-- | unpixelterm.py | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/unpixelterm.py b/unpixelterm.py index 234d3d1..d9b1498 100644 --- a/unpixelterm.py +++ b/unpixelterm.py @@ -2,22 +2,13 @@ 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 +import xtermcolors 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 @@ -25,7 +16,7 @@ def parse_escape_sequence(seq): while i<len(codes): if codes[i] in [38, 48]: if codes[i+1] == 5: - c = formatter.xterm_colors[codes[i+2]] + c = xtermcolors.xterm_colors[codes[i+2]] fg, bg = (c, bg) if codes[i] == 38 else (fg, c) i += 2 elif codes[i] == 39: @@ -99,3 +90,29 @@ def unpixelterm(text): x, y = 0, y+2 return img, metadata +if __name__ == '__main__': + import argparse, json + + parser = argparse.ArgumentParser(description='Convert images rendered by pixelterm-like utilities back to PNG') + parser.add_argument('-v', '--verbose', action='store_true') + output_group = parser.add_mutually_exclusive_group() + output_group.add_argument('-o', '--output', type=str, help='Output file name, defaults to ${input%.pony}.png') + output_group.add_argument('-d', '--output-dir', type=str, help='Place output files here') + parser.add_argument('input', type=argparse.FileType('r'), nargs='+') + args = parser.parse_args() + if len(args.input) > 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()) + pnginfo = PngImagePlugin.PngInfo() + pnginfo.add_text('pixelterm-metadata', json.dumps(metadata)) + foo, _, _ = f.name.rpartition('.pony') + output = args.output or foo+'.png' + if args.output_dir: + output = os.path.join(args.output_dir, os.path.basename(output)) + img.save(output, 'PNG', pnginfo=pnginfo) |