X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Fpure_image.py;h=8f027bd627fe0b62893076fd920de8bcaa12d982;hp=8e76c95a11d7e37718ec6bb0e6984fe286a35259;hb=1adf5dd916956546205cae0b7baa37f4faeaf5fa;hpb=f5677fb1d3a9e2aa06971500b2c244dcdbe43a3f diff --git a/rainbowstream/pure_image.py b/rainbowstream/pure_image.py index 8e76c95..8f027bd 100644 --- a/rainbowstream/pure_image.py +++ b/rainbowstream/pure_image.py @@ -1,6 +1,7 @@ from PIL import Image from functools import partial from .config import * +from .py3patch import * import sys import os @@ -8,7 +9,7 @@ import os """ This file is borrowed from following gist: https://gist.github.com/MicahElliott/719710 -It's too slow in compare with C program, so bot be used here +It's too slow in compare with C program. """ CLUT = [ # color look-up table @@ -281,6 +282,9 @@ CLUT = [ # color look-up table def _create_dicts(): + """ + Create dictionary + """ short2rgb_dict = dict(CLUT) rgb2short_dict = {} for k, v in short2rgb_dict.items(): @@ -291,24 +295,39 @@ RGB2SHORT_DICT, SHORT2RGB_DICT = _create_dicts() def short2rgb(short): + """ + Short to RGB + """ return SHORT2RGB_DICT[short] def pixel_print(ansicolor): + """ + Print a pixel with given Ansi color + """ sys.stdout.write('\033[48;5;%sm \033[0m' % (ansicolor)) def hex_to_rgb(value): + """ + Hex to RGB + """ value = value.lstrip('#') lv = len(value) - return tuple(int(value[i:i + lv / 3], 16) for i in range(0, lv, lv / 3)) + return tuple(int(value[i:i + lv / 3], 16) for i in xrange(0, lv, lv / 3)) def rgb_to_hex(rgb): + """ + RGB to Hex + """ return '%02x%02x%02x' % rgb def rgb2short(r, g, b): + """ + RGB to short + """ dist = lambda s, d: (s[0] - d[0]) ** 2 + \ (s[1] - d[1]) ** 2 + (s[2] - d[2]) ** 2 ary = [hex_to_rgb(hex) for hex in RGB2SHORT_DICT] @@ -316,25 +335,36 @@ def rgb2short(r, g, b): return RGB2SHORT_DICT[rgb_to_hex(m)] -def image_to_display(path): +def image_to_display(path, start=None, length=None): + """ + Display an image + """ + rows, columns = os.popen('stty size', 'r').read().split() + if not start: + start = IMAGE_SHIFT + if not length: + length = int(columns) - 2 * start i = Image.open(path) i = i.convert('RGBA') w, h = i.size i.load() - rows, columns = os.popen('stty size', 'r').read().split() - width = min(w, int(columns) - 2 * IMAGE_SHIFT) + width = min(w, length) height = int(float(h) * (float(width) / float(w))) height //= 2 i = i.resize((width, height), Image.BICUBIC) height = min(height, IMAGE_MAX_HEIGHT) for y in xrange(height): - print ' ' * IMAGE_SHIFT, + sys.stdout.write(' ' * start) for x in xrange(width): p = i.getpixel((x, y)) r, g, b = p[:3] pixel_print(rgb2short(r, g, b)) - print '' + sys.stdout.write('\n') + +""" +For direct using purpose +""" if __name__ == '__main__': image_to_display(sys.argv[1])