archive

- Random tools & helpful resources for IRC
git clone git://git.acid.vegas/archive.git
Log | Files | Refs | Archive

img2irc_opencv.py (6240B)

      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 + opencv implementation
      8 '''
      9 
     10 try:
     11 	import cv2
     12 except ImportError:
     13 	raise SystemExit('missing required \'opencv-python\' library (https://pypi.org/project/opencv-python/)')
     14 try:
     15 	import numpy as np
     16 except ImportError:
     17 	raise SystemExit('missing required \'numpy\' library (https://pypi.org/project/numpy/)')
     18 
     19 palettes = {
     20 	'RGB16': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
     21 			  0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff,      0x0,      0x0],
     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 	'RGB99': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
     34 			  0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2,
     35 			  0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
     36 			  0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400,
     37 			  0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045,
     38 			  0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5,
     39 			  0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00,
     40 			  0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098,
     41 			  0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff,
     42 			  0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c,
     43 			  0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3,
     44 			  0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f,
     45 			  0xbcbcbc, 0xe2e2e2, 0xffffff]
     46 }
     47 
     48 def convert(data, max_line_len, img_width=80, palette='RGB99', quantize_colors=None):
     49 	if palette not in palettes:
     50 		raise Exception('invalid palette option')
     51 	palette = palettes[palette]
     52 	np_arr = np.asarray(bytearray(data), dtype="uint8")
     53 	image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
     54 	del data
     55 	return convert_image(image, quantize_colors, max_line_len, img_width, palette)
     56 
     57 def convert_image(orig_image, quantize_colors, max_line_len, img_width, palette):
     58 	image = ircize(orig_image, img_width, quantize_colors)
     59 	CHAR = '\u2580'
     60 	buf = list()
     61 	for i in range(0, image.shape[0], 2):
     62 		if i+1 >= image.shape[0]:
     63 			bitmap = [[bgr_to_hex(image[i, x]) for x in range(image.shape[1])]]
     64 			bitmap += [[0 for _ in range(image.shape[1])]]
     65 		else:
     66 			bitmap = [[bgr_to_hex(image[y, x]) for x in range(image.shape[1])] for y in [i, i+1]]
     67 		top_row = [AnsiPixel(px, palette) for px in bitmap[0]]
     68 		bottom_row = [AnsiPixel(px, palette) for px in bitmap[1]]
     69 		buf += [""]
     70 		last_fg = last_bg = -1
     71 		ansi_row = list()
     72 		for j in range(image.shape[1]):
     73 			top_pixel = top_row[j]
     74 			bottom_pixel = bottom_row[j]
     75 			pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel)
     76 			fg = pixel_pair.top.irc
     77 			bg = pixel_pair.bottom.irc
     78 			if j != 0:
     79 				if fg == last_fg and bg == last_bg:
     80 					buf[-1] += CHAR
     81 				elif bg == last_bg:
     82 					buf[-1] += f'\x03{fg}{CHAR}'
     83 				else:
     84 					buf[-1] += f'\x03{fg},{bg}{CHAR}'
     85 			else:
     86 				buf[-1] += f'\x03{fg},{bg}{CHAR}'
     87 			last_fg = fg
     88 			last_bg = bg
     89 		if len(buf[-1].encode('utf-8', 'ignore')) > max_line_len:
     90 			if img_width - 5 < 5:
     91 				raise Exception('image would get too small')
     92 			return convert_image(orig_image, quantize_colors, max_line_len, img_width-5, palette)
     93 	return buf
     94 
     95 def ircize(image, img_width, quantize_colors):
     96 	(height, width, _) = image.shape
     97 	img_height = img_width / width * height
     98 	image = cv2.resize(image, (int(img_width), int(img_height)), interpolation=cv2.INTER_AREA)
     99 	brightness = np.sum(image) / (255 * image.shape[0] * image.shape[1])
    100 	minimum_brightness = 0.72
    101 	ratio = brightness / minimum_brightness
    102 	if ratio < 1:
    103 		image = cv2.convertScaleAbs(image, alpha = 1 / ratio, beta = 0)
    104 	imgf = np.float32(image).reshape(-1, 3)
    105 	criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,20,2.0)
    106 	compactness, label, center = cv2.kmeans(imgf, quantize_colors, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    107 	center = np.uint8(center)
    108 	final_img = center[label.flatten()]
    109 	image = final_img.reshape(image.shape)
    110 	return image
    111 
    112 def hex_to_rgb(color):
    113 	r = (color >> 16) & 255
    114 	g = (color >> 8) & 255
    115 	b = color & 255
    116 	return (r,g,b)
    117 
    118 def rgb_to_hex(rgb):
    119 	if len(list(rgb)) < 3:
    120 		r = g = b = rgb[0]
    121 	else:
    122 		r = rgb[0]
    123 		g = rgb[1]
    124 		b = rgb[2]
    125 
    126 	return (r << 16) + (g << 8) + b
    127 
    128 def bgr_to_hex(bgr):
    129 	return rgb_to_hex((bgr[0], 0, 0)) if len(list(bgr)) < 3 else rgb_to_hex((bgr[2], bgr[1], bgr[0]))
    130 
    131 def color_distance_squared(c1, c2):
    132 	d1 = abs(c1[0] - c2[0])
    133 	d2 = abs(c1[1] - c2[1])
    134 	d3 = abs(c1[2] - c2[2])
    135 	return d1 * d1 + d2 * d2 + d3 * d3
    136 
    137 class AnsiPixel:
    138 	def __init__(self, pixel_u32, palette):
    139 		self.irc = self.nearest_hex_color(pixel_u32, palette)
    140 
    141 	def nearest_hex_color(self, pixel_u32, hex_colors):
    142 		rgb_colors = [hex_to_rgb(color) for color in hex_colors]
    143 		rgb_colors.sort(key=lambda rgb: color_distance_squared(hex_to_rgb(pixel_u32), rgb))
    144 		hex_color = rgb_to_hex(rgb_colors[0])
    145 		return hex_colors.index(hex_color)
    146 
    147 class AnsiPixelPair:
    148 	def __init__(self, top, bottom):
    149 		self.top = top
    150 		self.bottom = bottom