fix bug
[rainbowstream.git] / rainbowstream / pure_image.py
index 0a75d7795755b8e8ae7d585fd67bd35c00a55940..5e953e9533c7b070579477c1126188e8a053e4d0 100644 (file)
-from PIL import Image,ImageFilter
+from PIL import Image
 from functools import partial
+from .config import *
 
-import sys,os
+import sys
+import os
+
+"""
+This file is borrowed from following gist:
+https://gist.github.com/MicahElliott/719710
+It's too slow in compare with C program.
+"""
 
 CLUT = [  # color look-up table
-#    8-bit, RGB hex
+    #    8-bit, RGB hex
 
     # Primary 3-bit (8 colors). Unique representation!
-    ('00',  '000000'),
-    ('01',  '800000'),
-    ('02',  '008000'),
-    ('03',  '808000'),
-    ('04',  '000080'),
-    ('05',  '800080'),
-    ('06',  '008080'),
-    ('07',  'c0c0c0'),
+    ('00', '000000'),
+    ('01', '800000'),
+    ('02', '008000'),
+    ('03', '808000'),
+    ('04', '000080'),
+    ('05', '800080'),
+    ('06', '008080'),
+    ('07', 'c0c0c0'),
 
     # Equivalent "bright" versions of original 8 colors.
-    ('08',  '808080'),
-    ('09',  'ff0000'),
-    ('10',  '00ff00'),
-    ('11',  'ffff00'),
-    ('12',  '0000ff'),
-    ('13',  'ff00ff'),
-    ('14',  '00ffff'),
-    ('15',  'ffffff'),
+    ('08', '808080'),
+    ('09', 'ff0000'),
+    ('10', '00ff00'),
+    ('11', 'ffff00'),
+    ('12', '0000ff'),
+    ('13', 'ff00ff'),
+    ('14', '00ffff'),
+    ('15', 'ffffff'),
 
     # Strictly ascending.
-    ('16',  '000000'),
-    ('17',  '00005f'),
-    ('18',  '000087'),
-    ('19',  '0000af'),
-    ('20',  '0000d7'),
-    ('21',  '0000ff'),
-    ('22',  '005f00'),
-    ('23',  '005f5f'),
-    ('24',  '005f87'),
-    ('25',  '005faf'),
-    ('26',  '005fd7'),
-    ('27',  '005fff'),
-    ('28',  '008700'),
-    ('29',  '00875f'),
-    ('30',  '008787'),
-    ('31',  '0087af'),
-    ('32',  '0087d7'),
-    ('33',  '0087ff'),
-    ('34',  '00af00'),
-    ('35',  '00af5f'),
-    ('36',  '00af87'),
-    ('37',  '00afaf'),
-    ('38',  '00afd7'),
-    ('39',  '00afff'),
-    ('40',  '00d700'),
-    ('41',  '00d75f'),
-    ('42',  '00d787'),
-    ('43',  '00d7af'),
-    ('44',  '00d7d7'),
-    ('45',  '00d7ff'),
-    ('46',  '00ff00'),
-    ('47',  '00ff5f'),
-    ('48',  '00ff87'),
-    ('49',  '00ffaf'),
-    ('50',  '00ffd7'),
-    ('51',  '00ffff'),
-    ('52',  '5f0000'),
-    ('53',  '5f005f'),
-    ('54',  '5f0087'),
-    ('55',  '5f00af'),
-    ('56',  '5f00d7'),
-    ('57',  '5f00ff'),
-    ('58',  '5f5f00'),
-    ('59',  '5f5f5f'),
-    ('60',  '5f5f87'),
-    ('61',  '5f5faf'),
-    ('62',  '5f5fd7'),
-    ('63',  '5f5fff'),
-    ('64',  '5f8700'),
-    ('65',  '5f875f'),
-    ('66',  '5f8787'),
-    ('67',  '5f87af'),
-    ('68',  '5f87d7'),
-    ('69',  '5f87ff'),
-    ('70',  '5faf00'),
-    ('71',  '5faf5f'),
-    ('72',  '5faf87'),
-    ('73',  '5fafaf'),
-    ('74',  '5fafd7'),
-    ('75',  '5fafff'),
-    ('76',  '5fd700'),
-    ('77',  '5fd75f'),
-    ('78',  '5fd787'),
-    ('79',  '5fd7af'),
-    ('80',  '5fd7d7'),
-    ('81',  '5fd7ff'),
-    ('82',  '5fff00'),
-    ('83',  '5fff5f'),
-    ('84',  '5fff87'),
-    ('85',  '5fffaf'),
-    ('86',  '5fffd7'),
-    ('87',  '5fffff'),
-    ('88',  '870000'),
-    ('89',  '87005f'),
-    ('90',  '870087'),
-    ('91',  '8700af'),
-    ('92',  '8700d7'),
-    ('93',  '8700ff'),
-    ('94',  '875f00'),
-    ('95',  '875f5f'),
-    ('96',  '875f87'),
-    ('97',  '875faf'),
-    ('98',  '875fd7'),
-    ('99',  '875fff'),
+    ('16', '000000'),
+    ('17', '00005f'),
+    ('18', '000087'),
+    ('19', '0000af'),
+    ('20', '0000d7'),
+    ('21', '0000ff'),
+    ('22', '005f00'),
+    ('23', '005f5f'),
+    ('24', '005f87'),
+    ('25', '005faf'),
+    ('26', '005fd7'),
+    ('27', '005fff'),
+    ('28', '008700'),
+    ('29', '00875f'),
+    ('30', '008787'),
+    ('31', '0087af'),
+    ('32', '0087d7'),
+    ('33', '0087ff'),
+    ('34', '00af00'),
+    ('35', '00af5f'),
+    ('36', '00af87'),
+    ('37', '00afaf'),
+    ('38', '00afd7'),
+    ('39', '00afff'),
+    ('40', '00d700'),
+    ('41', '00d75f'),
+    ('42', '00d787'),
+    ('43', '00d7af'),
+    ('44', '00d7d7'),
+    ('45', '00d7ff'),
+    ('46', '00ff00'),
+    ('47', '00ff5f'),
+    ('48', '00ff87'),
+    ('49', '00ffaf'),
+    ('50', '00ffd7'),
+    ('51', '00ffff'),
+    ('52', '5f0000'),
+    ('53', '5f005f'),
+    ('54', '5f0087'),
+    ('55', '5f00af'),
+    ('56', '5f00d7'),
+    ('57', '5f00ff'),
+    ('58', '5f5f00'),
+    ('59', '5f5f5f'),
+    ('60', '5f5f87'),
+    ('61', '5f5faf'),
+    ('62', '5f5fd7'),
+    ('63', '5f5fff'),
+    ('64', '5f8700'),
+    ('65', '5f875f'),
+    ('66', '5f8787'),
+    ('67', '5f87af'),
+    ('68', '5f87d7'),
+    ('69', '5f87ff'),
+    ('70', '5faf00'),
+    ('71', '5faf5f'),
+    ('72', '5faf87'),
+    ('73', '5fafaf'),
+    ('74', '5fafd7'),
+    ('75', '5fafff'),
+    ('76', '5fd700'),
+    ('77', '5fd75f'),
+    ('78', '5fd787'),
+    ('79', '5fd7af'),
+    ('80', '5fd7d7'),
+    ('81', '5fd7ff'),
+    ('82', '5fff00'),
+    ('83', '5fff5f'),
+    ('84', '5fff87'),
+    ('85', '5fffaf'),
+    ('86', '5fffd7'),
+    ('87', '5fffff'),
+    ('88', '870000'),
+    ('89', '87005f'),
+    ('90', '870087'),
+    ('91', '8700af'),
+    ('92', '8700d7'),
+    ('93', '8700ff'),
+    ('94', '875f00'),
+    ('95', '875f5f'),
+    ('96', '875f87'),
+    ('97', '875faf'),
+    ('98', '875fd7'),
+    ('99', '875fff'),
     ('100', '878700'),
     ('101', '87875f'),
     ('102', '878787'),
@@ -271,6 +279,7 @@ CLUT = [  # color look-up table
     ('255', 'eeeeee'),
 ]
 
+
 def _create_dicts():
     short2rgb_dict = dict(CLUT)
     rgb2short_dict = {}
@@ -292,7 +301,7 @@ def pixel_print(ansicolor):
 def hex_to_rgb(value):
     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 range(0, lv, lv / 3))
 
 
 def rgb_to_hex(rgb):
@@ -300,31 +309,36 @@ def rgb_to_hex(rgb):
 
 
 def rgb2short(r, g, b):
-    dist = lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2+(s[2]-d[2])**2
+    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]
-    m = min(ary, key=partial(dist, (r,g,b)))
+    m = min(ary, key=partial(dist, (r, g, b)))
     return RGB2SHORT_DICT[rgb_to_hex(m)]
 
-def image_to_display(path):
-    i = Image.open(path) 
+
+def image_to_display(path,start=None,length=None):
+    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
+    w, h = i.size
     i.load()
-    rows, columns = os.popen('stty size', 'r').read().split()
-    width = min(w, int(columns)-2*6)
+    width = min(w, length)
     height = int(float(h) * (float(width) / float(w)))
     height //= 2
     i = i.resize((width, height), Image.BICUBIC)
-    height = min(height,30)
+    height = min(height, IMAGE_MAX_HEIGHT)
 
     for y in xrange(height):
-        print ' '*6 ,
+        print ' ' * start,
         for x in xrange(width):
-            p = i.getpixel((x,y))
+            p = i.getpixel((x, y))
             r, g, b = p[:3]
-            pixel_print(rgb2short(r,g,b))
+            pixel_print(rgb2short(r, g, b))
         print ''
 
 if __name__ == '__main__':
     image_to_display(sys.argv[1])
-