Removed standard colors 0-15 for portability. Removed pygments dep. Fixed setup.py
This commit is contained in:
parent
76579920ef
commit
5e763123f8
11 changed files with 109 additions and 117 deletions
47
pixelterm.py
47
pixelterm.py
|
|
@ -1,13 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
#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
|
||||
import xtermcolors
|
||||
|
||||
formatter = terminal256.Terminal256Formatter()
|
||||
reset_sequence = terminal256.EscapeSequence(fg=formatter._closest_color(0,0,0), bg=formatter._closest_color(0,0,0)).reset_string()
|
||||
reset_sequence = '\033[39;49m'
|
||||
|
||||
def termify_pixels(img):
|
||||
sx, sy = img.size
|
||||
|
|
@ -25,7 +20,7 @@ def termify_pixels(img):
|
|||
if color in bgd:
|
||||
return bgd[color]
|
||||
r,g,b,_ = color
|
||||
bgd[color] = '\033[48;5;'+str(formatter._closest_color(r,g,b))+'m'
|
||||
bgd[color] = '\033[48;5;'+str(xtermcolors.closest_color(r,g,b))+'m'
|
||||
return bgd[color]
|
||||
|
||||
def fgescape(color):
|
||||
|
|
@ -34,7 +29,7 @@ def termify_pixels(img):
|
|||
return ''
|
||||
fg=color
|
||||
r,g,b,_ = color
|
||||
fgd[color] = '\033[38;5;'+str(formatter._closest_color(r,g,b))+'m'
|
||||
fgd[color] = '\033[38;5;'+str(xtermcolors.closest_color(r,g,b))+'m'
|
||||
return fgd[color]
|
||||
|
||||
def balloon(x,y):
|
||||
|
|
@ -74,3 +69,37 @@ 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__':
|
||||
import os, sys, argparse, os.path, json
|
||||
from multiprocessing import Pool
|
||||
from PIL import Image, PngImagePlugin
|
||||
|
||||
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()
|
||||
|
||||
def convert(f):
|
||||
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.get('_comment')
|
||||
if comment is not None:
|
||||
del metadata['_comment']
|
||||
comment = '\n'+comment
|
||||
else:
|
||||
comment = ''
|
||||
metadataarea = '$$$\n' +\
|
||||
'\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\
|
||||
comment + '\n$$$\n'
|
||||
with open(output, 'w') as of:
|
||||
of.write(metadataarea)
|
||||
of.write(termify_pixels(img))
|
||||
else:
|
||||
print(termify_pixels(img))
|
||||
|
||||
p = Pool()
|
||||
p.map(convert, args.image)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue