Created
April 1, 2014 17:34
-
-
Save lesguillemets/9919022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding:utf-8 | |
from textwrap import dedent | |
import PIL.Image as img | |
import numpy as np | |
import sys | |
from rgbtoansi import colorize_bg | |
def get_termsize(): | |
import fcntl, termios, struct | |
h, w, _, _ = struct.unpack( | |
'HHHH', | |
fcntl.ioctl( | |
sys.stdout.fileno(), | |
termios.TIOCGWINSZ, | |
struct.pack('HHHH', 0, 0, 0, 0))) | |
return w, h | |
def show_img(imgfile, widthratio=1/2, fontratio=2, method="upperleft"): | |
''' | |
imgfile : image file readable by PIL.Image.open | |
widthratio : how much of terminal width the image uses | |
fontratio : the ratio height/width of your font | |
about 2 for monaco, 2.5 for ubuntu mono. | |
''' | |
w, _ = get_termsize() | |
w = int(w*widthratio) | |
imgary = np.array(img.open(imgfile)) | |
imgheight, imgwidth = len(imgary), len(imgary[0]) | |
marks = [int(i*imgwidth/w) for i in range(w+1)] | |
rowpixels = (imgwidth/w)*fontratio # 2 for monaco, 2.5 for ubuntu mono | |
yreadingstart = 0 | |
while yreadingstart < imgheight-1: | |
for (i,m) in enumerate(marks[1:]): | |
m_b = marks[i] | |
if method == "upperleft": | |
pixel = imgary[yreadingstart,m_b] | |
elif method == "mean": | |
grid = imgary[yreadingstart:yreadingstart+rowpixels, | |
m_b:m] | |
pixel = np.array((0,0,0)) | |
for y_ in range(len(grid)): | |
for x_ in range(len(grid[0])): | |
pixel += grid[y_,x_] | |
pixel = pixel//(len(grid)*len(grid[0])) | |
sys.stdout.write(colorize_bg(' ', list(pixel))) | |
sys.stdout.flush() | |
yreadingstart += rowpixels | |
sys.stdout.write('\n') | |
def test(): | |
testfile = 'kitten.jpg' | |
show_img(testfile,1/2,2) | |
print("______________________________") | |
show_img(testfile,1/2,2, 'mean') | |
def main(): | |
try: | |
filename = sys.argv[1] | |
except IndexError: | |
print("specify filename.") | |
return | |
show_img(filename) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding:utf-8 | |
from __future__ import print_function # for compatibility | |
from textwrap import dedent | |
def rgb_to_code(rgb): | |
'''\ | |
r,g,b should be given in 0-255 integer. | |
''' | |
if not all(map(lambda x: 0<=x<=255, rgb)): | |
raise ValueError(dedent( | |
"""\ | |
rgb should be given as a list of 0-255 integers. | |
you gave : {} | |
""".format(rgb))) | |
rlevel, glevel, blevel = map(lambda x:x*6//256, rgb) | |
return 16 + rlevel*36 + glevel*6 + blevel | |
def colorize_fg(string, rgb): | |
return "\033[38;5;{}m{}\033[0m".format( | |
rgb_to_code(rgb),string) | |
def colorize_bg(string, rgb): | |
return "\033[48;5;{}m{}\033[0m".format( | |
rgb_to_code(rgb),string) | |
def fgbegin(rgb): | |
return "\033[38;5;{}m".format( | |
rgb_to_code(rgb)) | |
def bgbegin(rgb): | |
return "\033[48;5;{}m".format( | |
rgb_to_code(rgb)) | |
def set_color(fg, bg): | |
colorcodes = [ | |
'{};5;{}'.format(fb, rgb_to_code(col)) | |
for (fb, col) in ((38,fg), (48,bg)) if col] | |
return '\033[{}m'.format(';'.join(colorcodes)) | |
if fg: | |
print("\033[38;5;{}m".format(fg), end='') | |
if bg: | |
print("\033[48;5;{}m".format(bg), end='') | |
def clear_style(): | |
return "\033[0m" | |
if __name__ == "__main__": | |
print(colorize_fg("there",(25,0,0))) | |
print(colorize_bg("there",(100,100,100))) | |
print(set_color((250,0,0),(0,255,0)),end='') | |
print("FOOOOOO ",end='') | |
print(clear_style()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment