archive- Random tools & helpful resources for IRC |
git clone git://git.acid.vegas/archive.git |
Log | Files | Refs | Archive |
img2irc.py (5233B)
1 #!/usr/bin/env python 2 # Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll) 3 4 ''' 5 Props: 6 - forked idea from malcom's img2irc (https://github.com/waveplate/img2irc) 7 - big props to wrk (wr34k) for forking this one 8 - contrast enhancement, effects, & RBG88 added by acidvegas 9 10 pull request: https://github.com/ircart/scroll/pull/3 11 12 ''' 13 14 import io 15 16 try: 17 from PIL import Image, ImageEnhance 18 except ImportError: 19 raise SystemExit('missing required \'pillow\' library (https://pypi.org/project/pillow/)') 20 21 palettes = { 22 'RGB88': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00, 23 0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x0, 0x0, 24 0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747, 25 0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400, 26 0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045, 27 0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5, 28 0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00, 29 0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098, 30 0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff, 31 0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c, 32 0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3], 33 34 'RGB99': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00, 35 0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2, 36 0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747, 37 0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400, 38 0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045, 39 0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5, 40 0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00, 41 0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098, 42 0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff, 43 0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c, 44 0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3, 45 0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f, 46 0xbcbcbc, 0xe2e2e2, 0xffffff] 47 } 48 49 def convert(data, max_line_len, img_width=80, palette='RGB99', enhance=False, effect=None): 50 if palette not in palettes: 51 raise Exception('invalid palette option') 52 palette = palettes[palette] 53 image = Image.open(io.BytesIO(data)) 54 if enhance: 55 image = ImageEnhance.Contrast(image) 56 if effect == 'grey': 57 image = image.convert("L") 58 elif effect == 'black': 59 image = image.convert("1") 60 del data 61 return convert_image(image, max_line_len, img_width, palette) 62 63 def convert_image(image, max_line_len, img_width, palette): 64 (width, height) = image.size 65 img_height = img_width / width * height 66 del height, width 67 image.thumbnail((img_width, img_height), Image.Resampling.LANCZOS) 68 del img_height 69 CHAR = '\u2580' 70 buf = list() 71 for i in range(0, image.size[1], 2): 72 if i+1 >= image.size[1]: 73 bitmap = [[rgb_to_hex(image.getpixel((x, i))) for x in range(image.size[0])]] 74 bitmap += [[0 for _ in range(image.size[0])]] 75 else: 76 bitmap = [[rgb_to_hex(image.getpixel((x, y))) for x in range(image.size[0])] for y in [i, i+1]] 77 top_row = [AnsiPixel(px, palette) for px in bitmap[0]] 78 bottom_row = [AnsiPixel(px, palette) for px in bitmap[1]] 79 buf += [""] 80 last_fg = last_bg = -1 81 ansi_row = list() 82 for j in range(image.size[0]): 83 top_pixel = top_row[j] 84 bottom_pixel = bottom_row[j] 85 pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel) 86 fg = pixel_pair.top.irc 87 bg = pixel_pair.bottom.irc 88 if j != 0: 89 if fg == last_fg and bg == last_bg: 90 buf[-1] += CHAR 91 elif bg == last_bg: 92 buf[-1] += f'\x03{fg}{CHAR}' 93 else: 94 buf[-1] += f'\x03{fg},{bg}{CHAR}' 95 else: 96 buf[-1] += f'\x03{fg},{bg}{CHAR}' 97 last_fg = fg 98 last_bg = bg 99 if len(buf[-1].encode('utf-8', 'ignore')) > max_line_len: 100 if img_width - 5 < 10: 101 raise Exception('internal error') 102 return convert_image(image, max_line_len, img_width-5, palette) 103 return buf 104 105 def hex_to_rgb(color): 106 r = color >> 16 107 g = (color >> 8) % 256 108 b = color % 256 109 return (r,g,b) 110 111 def rgb_to_hex(rgb): 112 r = rgb[0] 113 g = rgb[1] 114 b = rgb[2] 115 return (r << 16) + (g << 8) + b 116 117 def color_distance_squared(c1, c2): 118 dr = c1[0] - c2[0] 119 dg = c1[1] - c2[1] 120 db = c1[2] - c2[2] 121 return dr * dr + dg * dg + db * db 122 123 class AnsiPixel: 124 def __init__(self, pixel_u32, palette): 125 self.irc = self.nearest_hex_color(pixel_u32, palette) 126 127 def nearest_hex_color(self, pixel_u32, hex_colors): 128 rgb_colors = [hex_to_rgb(color) for color in hex_colors] 129 rgb_colors.sort(key=lambda rgb: color_distance_squared(hex_to_rgb(pixel_u32), rgb)) 130 hex_color = rgb_to_hex(rgb_colors[0]) 131 return hex_colors.index(hex_color) 132 133 class AnsiPixelPair: 134 def __init__(self, top, bottom): 135 self.top = top 136 self.bottom = bottom