From 4493e514b3f0c36233a58fb03ebfc4f45574ee7c Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 2 Apr 2013 02:32:36 +0200 Subject: Color lookup optimization and final color reset added --- pixelterm.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pixelterm.py b/pixelterm.py index 3ee0d85..89866a7 100755 --- a/pixelterm.py +++ b/pixelterm.py @@ -7,24 +7,31 @@ from PIL import Image, PngImagePlugin def termify_pixels(img): sx, sy = img.size out = '' + formatter = terminal256.Terminal256Formatter() fg,bg = None,None + fgd,bgd = {},{} def bgescape(color): nonlocal bg if bg == color: return '' bg=color r,g,b,a = color + if color in bgd: + return bgd[color] + bgd[color] = terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string() if color == (0,0,0,0): - return terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string() - return terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string() + bgd[color] = terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string() + return bgd[color] + def fgescape(color): nonlocal fg if fg == color: return '' fg=color r,g,b,_ = color - return terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string() + fgd[color]= terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string() + return fgd[color] #NOTE: This ignores the last line if there is an odd number of lines. for y in range(0, sy-1, 2): for x in range(sx): @@ -43,6 +50,7 @@ def termify_pixels(img): c,te,be = cf,te,te out += te(coltop) + be(colbot) + c out += '\n' + out += terminal256.EscapeSequence(fg=formatter._closest_color(0,0,0), bg=formatter._closest_color(0,0,0)).reset_string() return out if __name__ == '__main__': -- cgit