From a82ff75260da230d9048883936fb13a02c18c0c4 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 7 Apr 2013 01:46:32 +0200 Subject: Performance optimizations --- pixelterm.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pixelterm.py b/pixelterm.py index a0569de..9f8dfb6 100755 --- a/pixelterm.py +++ b/pixelterm.py @@ -18,39 +18,37 @@ def termify_pixels(img): fg,bg = None,None fgd,bgd = {},{} def bgescape(color): - nonlocal bg + nonlocal bg, bgd if bg == color: return '' bg=color - r,g,b,a = color + if color == (0,0,0,0): + return '\033[49m' 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): - bgd[color] = terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string() + bgd[color] = '\033[48;5;'+str(formatter._closest_color(color))+'m' return bgd[color] def fgescape(color): - nonlocal fg + nonlocal fg, fgd if fg == color: return '' fg=color - r,g,b,_ = color - fgd[color]= terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string() + fgd[color] = '\033[38;5;'+str(formatter._closest_color(color))+'m' return fgd[color] def balloon(x,y): - if x+1 == img.size[0] or img.getpixel((x+1, y)) != (0,255,0,127): + if x+1 == img.size[0] or img.im.getpixel((x+1, y)) != (0,255,0,127): w = 1 - while x-w >= 0 and img.getpixel((x-w, y)) == (0,255,0,127): + while x-w >= 0 and img.im.getpixel((x-w, y)) == (0,255,0,127): w += 1 return '$balloon{}$'.format(w) return '' for y in range(0, sy, 2): for x in range(sx): - coltop = img.getpixel((x, y)) - colbot = img.getpixel((x, y+1)) if y+1 < img.size[1] else (0,0,0,0) + coltop = img.im.getpixel((x, y)) + colbot = img.im.getpixel((x, y+1)) if y+1 < img.size[1] else (0,0,0,0) if coltop[3] == 127: #Control colors out += reset_sequence -- cgit