aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README.md1
-rwxr-xr-xgifterm.py73
3 files changed, 73 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index c1bd3b8..98d8ec7 100644
--- a/Makefile
+++ b/Makefile
@@ -3,11 +3,13 @@ PREFIX?=/usr/local
install:
install -m 0755 pixelterm.py $(PREFIX)/bin/pixelterm
+ install -m 0755 gifterm.py $(PREFIX)/bin/gifterm
install -m 0755 unpixelterm.py $(PREFIX)/bin/unpixelterm
install -m 0755 pngmeta.py $(PREFIX)/bin/pngmeta
uninstall:
rm $(PREFIX)/bin/pixelterm
+ rm $(PREFIX)/bin/gifterm
rm $(PREFIX)/bin/unpixelterm
rm $(PREFIX)/bin/pngmeta
diff --git a/README.md b/README.md
index 9637242..59d81c6 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,7 @@ Type ```sudo make install```, which will copy pixelterm.py to ```/usr/local/bin/
Usage
-----
```pixelterm FILE```
+```gifterm FILE```
Advanced usage
--------------
diff --git a/gifterm.py b/gifterm.py
index a93e224..cef85b9 100755
--- a/gifterm.py
+++ b/gifterm.py
@@ -7,12 +7,79 @@ import os, sys, argparse, os.path, json, time, signal, atexit
#(but don't forget to send me a pull request ;)
from pygments.formatters import terminal256
from PIL import Image, GifImagePlugin, ImageSequence
-import pixelterm
clear_screen = '\033[H\033[2J'
cursor_invisible = '\033[?25l'
cursor_visible = '\033[?25h'
+formatter = terminal256.Terminal256Formatter()
+reset_sequence = terminal256.EscapeSequence(fg=formatter._closest_color(0,0,0), bg=formatter._closest_color(0,0,0)).reset_string()
+
+def termify_pixels(img):
+ sx, sy = img.size
+ out = ''
+
+ fg,bg = None,None
+ fgd,bgd = {},{}
+ def bgescape(color):
+ nonlocal bg, bgd
+ if bg == color:
+ return ''
+ bg=color
+ if color == (0,0,0,0):
+ return '\033[49m'
+ if color in bgd:
+ return bgd[color]
+ r,g,b,_ = color
+ bgd[color] = '\033[48;5;'+str(formatter._closest_color(r,g,b))+'m'
+ return bgd[color]
+
+ def fgescape(color):
+ nonlocal fg, fgd
+ if fg == color:
+ return ''
+ fg=color
+ r,g,b,_ = color
+ fgd[color] = '\033[38;5;'+str(formatter._closest_color(r,g,b))+'m'
+ return fgd[color]
+
+ def balloon(x,y):
+ 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.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.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
+ out += {(255, 0, 0, 127): lambda x,y:'$\\$',
+ (0, 0, 255, 127): lambda x,y:'$/$',
+ (0, 255, 0, 127): balloon
+ }.get(coltop, lambda x,y:' ')(x,y)
+ continue
+
+ if coltop[3] != 255:
+ coltop = (0,0,0,0)
+ if colbot[3] != 255:
+ colbot = (0,0,0,0)
+
+ #Da magicks: ▀█▄
+ c,cf = '▀','█'
+ te,be = fgescape,bgescape
+ if coltop == (0,0,0,0) or ((coltop == bg or colbot == fg) and not colbot == (0,0,0,0)):
+ c,cf,te,be = '▄',' ',be,te
+ if colbot == coltop:
+ c,te,be = cf,te,te
+ out += te(coltop) + be(colbot) + c
+ 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)
@@ -42,7 +109,7 @@ if __name__ == '__main__':
im = last_frame.copy()
im.thumbnail((tw, th), Image.NEAREST)
- frames.append(pixelterm.termify_pixels(im))
+ frames.append(termify_pixels(im))
print(cursor_invisible)
atexit.register(lambda:print(cursor_visible))
@@ -50,7 +117,7 @@ if __name__ == '__main__':
while True:
for frame in frames:
- print(clear_screen, pixelterm.reset_sequence)
+ print(clear_screen, reset_sequence)
print(frame)
time.sleep(img.info['duration']/1000.0)