aboutsummaryrefslogtreecommitdiff
path: root/unpixelterm
diff options
context:
space:
mode:
authorjaseg <s@jaseg.de>2013-07-29 18:51:18 +0200
committerjaseg <s@jaseg.de>2013-07-29 18:51:18 +0200
commita79ab969533320582482856758394e74739b152e (patch)
tree59e320c15a282cb57318683815c8e3d529e2f087 /unpixelterm
parent62e62f1eace5899d0176ef0dd1b66c6ef72f7ab0 (diff)
downloadpixelterm-a79ab969533320582482856758394e74739b152e.tar.gz
pixelterm-a79ab969533320582482856758394e74739b152e.tar.bz2
pixelterm-a79ab969533320582482856758394e74739b152e.zip
Made a proper python package out of this.
Diffstat (limited to 'unpixelterm')
-rwxr-xr-xunpixelterm38
1 files changed, 38 insertions, 0 deletions
diff --git a/unpixelterm b/unpixelterm
new file mode 100755
index 0000000..4556d07
--- /dev/null
+++ b/unpixelterm
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+import os, sys, argparse, os.path, json, unpixelterm
+#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
+
+if __name__ == '__main__':
+ 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.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)