From 6566bce064dfa4dcd1b773c878e54450ffa86424 Mon Sep 17 00:00:00 2001 From: acidvegas Date: Mon, 26 Jun 2023 05:32:13 -0400 Subject: [PATCH] img2irc improvements (props wrk) and fixed img_width --- __pycache__/img2irc.cpython-39.pyc | Bin 0 -> 4854 bytes img2irc.py | 76 ++++++++++++++--------------- scroll.py | 14 +++++- 3 files changed, 49 insertions(+), 41 deletions(-) create mode 100644 __pycache__/img2irc.cpython-39.pyc diff --git a/__pycache__/img2irc.cpython-39.pyc b/__pycache__/img2irc.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..78ca4a73ac604b3c703329e54f02c761da711b06 GIT binary patch literal 4854 zcma)AO^g)B6|So8`Q6$7HL%!8##Ry-2X+XKuuaI?{28N|1+x+kt)NctRL^Yh_Ds)K z_rmU~2VyUhvE)cZj;ur`f{l~|mmDHn%7NT+$te-sIysR`_MDq#Y4g46*@a>8&n)%o zRn@CkuU@^s#_8`bGW;G`zmrpgjQxiOkN@%*yp9&W2OycnB$vYH7GD_xX859MSO#bB zu-0yqx#sdA&gjq0NOP6H$1k#$IKbqX%*xz#Ze?X&7OpcZCyTNVeO{JiKl*|kkb~%p zrM_PJX|d7FV_P#@5-_#U8h`EL9^U+ z{94f56PCSZ?UU4qFZQqI9YL}`)yk@*cxlNqQQBc03 z=F5ApsF!{;U!g@&_LO~ZMmcKv(G}XuZEqq_jVi>P zb8AsGrL0!g&w**pAlBWVi9UmN94-7dK*a9wNZc`Ew#L_Z$GFNOGd5x#i&bNq-Q`o2 zj1r(r%%JVI65$0&?$~nVhNsUa{k5QV*;SG4k;oM@F{KkZNvY{9*}m6uZ67-d(4~FF zlhJIF(R6p2>IcKm4^(G^rdxGtp1kZfoUkh0%k&!6UIlfZVxqmAgn#)r7z z3ijE1OCliirJILuK>Qngj{RJ8jE)(L*m#!3<{0Z_u9{bk8~hIL8fWSv5_86!xh6Qf znvuMdjWVmOlZ&#nca?J%<)k278siK`Y@A>>`KAAUz;0k>r#^vJ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[1]: + bitmap = [[rgb_to_hex(image.getpixel((x, i))) for x in range(image.size[0])]] + bitmap += [[0 for _ in range(image.size[0])]] + else: + bitmap = [[rgb_to_hex(image.getpixel((x, y))) for x in range(image.size[0])] for y in [i, i+1]] + top_row = [AnsiPixel(px) for px in bitmap[0]] + bottom_row = [AnsiPixel(px) for px in bitmap[1]] + buf += [""] + last_fg = last_bg = -1 + ansi_row = list() + for j in range(image.size[0]): + top_pixel = top_row[j] + bottom_pixel = bottom_row[j] + pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel) fg = pixel_pair.top.irc bg = pixel_pair.bottom.irc - if x != 0: + if j != 0: if fg == last_fg and bg == last_bg: - buf += CHAR + buf[-1] += CHAR elif bg == last_bg: - buf += f'\x03{fg}{CHAR}' + buf[-1] += f'\x03{fg}{CHAR}' else: - buf += f'\x03{fg},{bg}{CHAR}' + buf[-1] += f'\x03{fg},{bg}{CHAR}' else: - buf += f'\x03{fg},{bg}{CHAR}' + buf[-1] += f'\x03{fg},{bg}{CHAR}' last_fg = fg last_bg = bg - if y != len(ansi_image.halfblocks) - 1: - buf += '\n' - else: - buf += '\x0f' - return buf.splitlines() + print(len(buf[-1].encode('utf-8', 'ignore')), max_line_len) + if len(buf[-1].encode('utf-8', 'ignore')) > max_line_len: + if img_width - 5 < 10: + raise Exception('internal error') + return convert_image(image, max_line_len, img_width-5) + return buf def hex_to_rgb(color): r = color >> 16 @@ -59,7 +73,9 @@ def hex_to_rgb(color): return (r,g,b) def rgb_to_hex(rgb): - (r,g,b) = rgb + r = rgb[0] + g = rgb[1] + b = rgb[2] return (r << 16) + (g << 8) + b def color_distance_squared(c1, c2): @@ -97,23 +113,3 @@ class AnsiPixelPair: def __init__(self, top, bottom): self.top = top self.bottom = bottom - -class AnsiImage: - def __init__(self, image): - self.bitmap = [[rgb_to_hex(image.getpixel((x, y))) for x in range(image.size[0])] for y in range(image.size[1])] - if len(self.bitmap) % 2 != 0: - self.bitmap.append([0 for x in range(image.size[0])]) - ansi_bitmap = [[AnsiPixel(y) for y in x] for x in self.bitmap] - ansi_canvas = list() - for two_rows in range(0, len(ansi_bitmap), 2): - top_row = ansi_bitmap[two_rows] - bottom_row = ansi_bitmap[two_rows+1] - ansi_row = list() - for i in range(len(self.bitmap[0])): - top_pixel = top_row[i] - bottom_pixel = bottom_row[i] - pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel) - ansi_row.append(pixel_pair) - ansi_canvas.append(ansi_row) - self.image = image - self.halfblocks = ansi_canvas \ No newline at end of file diff --git a/scroll.py b/scroll.py index d8c4a12..ad2f538 100644 --- a/scroll.py +++ b/scroll.py @@ -82,6 +82,7 @@ class Bot(): self.db = None self.last = time.time() self.loops = dict() + self.host = '' self.playing = False self.settings = {'flood':1, 'ignore':'big,birds,doc,gorf,hang,nazi,pokemon', 'lines':500, 'msg':0.03, 'paste':True, 'png_width':80, 'results':25} self.slow = False @@ -201,6 +202,11 @@ class Bot(): await self.raw(f'JOIN {connection.channel} {connection.key}') if connection.key else await self.raw('JOIN ' + connection.channel) await self.raw('JOIN #scroll') await self.sync() + elif args[1] == '311' and len(args) >= 6: # RPL_WHOISUSER + nick = args[2] + host = args[5] + if nick == identity.nickname: + self.host = host elif args[1] == '433': error('The bot is already running or nick is in use.') elif args[1] == 'INVITE' and len(args) == 4: @@ -208,6 +214,11 @@ class Bot(): chan = args[3][1:] if invited == identity.nickname and chan in (connection.channel, '#scroll'): await self.raw(f'JOIN {connection.channel} {connection.key}') if connection.key else await self.raw('JOIN ' + connection.channel) + elif args[1] == 'JOIN' and len(args) >= 3: + nick = args[0].split('!')[0][1:] + host = args[0].split('@')[1] + if nick == identity.nickname: + self.host = host elif args[1] == 'KICK' and len(args) >= 4: chan = args[2] kicked = args[3] @@ -241,10 +252,11 @@ class Bot(): await asyncio.sleep(self.settings['msg']) elif args[1] == 'img' and len(args) == 3: url = args[2] + width = 512 - len(line.split(' :')[0])+4 if url.startswith('https://') or url.startswith('http://'): try: content = get_url(url).read() - ascii = await img2irc.convert(content, int(self.settings['png_width'])) + ascii = await img2irc.convert(content, 512 - len(f":{identity.nickname}!{identity.username}@{self.host} PRIVMSG {chan} :\r\n"), int(self.settings['png_width'])) except Exception as ex: await self.irc_error(chan, 'failed to convert image', ex) else: