Made a proper python package out of this.
This commit is contained in:
parent
62e62f1eac
commit
a79ab96953
11 changed files with 176 additions and 173 deletions
25
pixelterm.py
25
pixelterm.py
|
|
@ -1,12 +1,10 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, argparse, os.path, json
|
||||
#NOTE: This script uses pygments for RGB->X256 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
|
||||
|
||||
formatter = terminal256.Terminal256Formatter()
|
||||
reset_sequence = terminal256.EscapeSequence(fg=formatter._closest_color(0,0,0), bg=formatter._closest_color(0,0,0)).reset_string()
|
||||
|
|
@ -76,26 +74,3 @@ def termify_pixels(img):
|
|||
out = (out.rstrip() if bg == (0,0,0,0) else out) + '\n'
|
||||
return out[:-1] + reset_sequence + '\n'
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Render pixel images on 256-color ANSI terminals')
|
||||
parser.add_argument('image', type=str, nargs='*')
|
||||
parser.add_argument('-d', '--output-dir', type=str, help='Output directory (if not given, output to stdout)')
|
||||
args = parser.parse_args()
|
||||
for f in args.image:
|
||||
img = Image.open(f).convert("RGBA")
|
||||
if args.output_dir:
|
||||
print(f)
|
||||
foo, _, _ = f.rpartition('.png')
|
||||
output = os.path.join(args.output_dir, os.path.basename(foo)+'.pony')
|
||||
metadata = json.loads(img.info.get('pixelterm-metadata'))
|
||||
comment = metadata['_comment']
|
||||
del metadata['_comment']
|
||||
metadataarea = '$$$\n' +\
|
||||
'\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\
|
||||
'\n' + comment +\
|
||||
'\n$$$\n'
|
||||
with open(output, 'w') as of:
|
||||
of.write(metadataarea)
|
||||
of.write(termify_pixels(img))
|
||||
else:
|
||||
print(termify_pixels(img))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue