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