diff options
author | jaseg <s@jaseg.de> | 2013-04-07 01:46:32 +0200 |
---|---|---|
committer | jaseg <s@jaseg.de> | 2013-04-07 01:46:32 +0200 |
commit | a82ff75260da230d9048883936fb13a02c18c0c4 (patch) | |
tree | 3ab43d6e6e7480609377bb9870e0da48199e1bd9 | |
parent | 1b9601833475bf6f4dbee024d90c76bce0d25342 (diff) | |
download | pixelterm-a82ff75260da230d9048883936fb13a02c18c0c4.tar.gz pixelterm-a82ff75260da230d9048883936fb13a02c18c0c4.tar.bz2 pixelterm-a82ff75260da230d9048883936fb13a02c18c0c4.zip |
Performance optimizations
-rwxr-xr-x | pixelterm.py | 22 |
1 files 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 |