1
mirror of git://git.acid.vegas/scroll.git synced 2024-11-07 08:46:45 +00:00

Added RBG88 color palette and option to change palette on the fly with .ascii settings

This commit is contained in:
Dionysus 2023-06-28 14:08:37 -04:00
parent 4040e4c2eb
commit 5388ae8856
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 40 additions and 26 deletions

View File

@ -17,12 +17,43 @@ try:
except ImportError: except ImportError:
raise SystemExit('missing required \'pillow\' library (https://pypi.org/project/pillow/)') raise SystemExit('missing required \'pillow\' library (https://pypi.org/project/pillow/)')
def convert(data, max_line_len, img_width=80): palettes = {
'RGB88': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x0, 0x0,
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400,
0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045,
0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5,
0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00,
0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098,
0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff,
0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c,
0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3]
'RGB99': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2,
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400,
0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045,
0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5,
0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00,
0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098,
0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff,
0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c,
0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3,
0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f,
0xbcbcbc, 0xe2e2e2, 0xffffff]
}
def convert(data, max_line_len, img_width=80, palette='RBG88'):
if palette not in palettes:
raise Exception('invalid palette option')
palette = palettes[palette]
image = Image.open(io.BytesIO(data)) image = Image.open(io.BytesIO(data))
del data del data
return convert_image(image, max_line_len, img_width) return convert_image(image, max_line_len, img_width, palette)
def convert_image(image, max_line_len, img_width): def convert_image(image, max_line_len, img_width, palette):
(width, height) = image.size (width, height) = image.size
img_height = img_width / width * height img_height = img_width / width * height
del height, width del height, width
@ -30,15 +61,14 @@ def convert_image(image, max_line_len, img_width):
del img_height del img_height
CHAR = '\u2580' CHAR = '\u2580'
buf = list() buf = list()
print(image.size)
for i in range(0, image.size[1], 2): for i in range(0, image.size[1], 2):
if i+1 >= image.size[1]: if i+1 >= image.size[1]:
bitmap = [[rgb_to_hex(image.getpixel((x, i))) for x in range(image.size[0])]] bitmap = [[rgb_to_hex(image.getpixel((x, i))) for x in range(image.size[0])]]
bitmap += [[0 for _ in range(image.size[0])]] bitmap += [[0 for _ in range(image.size[0])]]
else: else:
bitmap = [[rgb_to_hex(image.getpixel((x, y))) for x in range(image.size[0])] for y in [i, i+1]] 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]] top_row = [AnsiPixel(px, palette) for px in bitmap[0]]
bottom_row = [AnsiPixel(px) for px in bitmap[1]] bottom_row = [AnsiPixel(px, palette) for px in bitmap[1]]
buf += [""] buf += [""]
last_fg = last_bg = -1 last_fg = last_bg = -1
ansi_row = list() ansi_row = list()
@ -59,7 +89,6 @@ def convert_image(image, max_line_len, img_width):
buf[-1] += f'\x03{fg},{bg}{CHAR}' buf[-1] += f'\x03{fg},{bg}{CHAR}'
last_fg = fg last_fg = fg
last_bg = bg last_bg = bg
print(len(buf[-1].encode('utf-8', 'ignore')), max_line_len)
if 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: if img_width - 5 < 10:
raise Exception('internal error') raise Exception('internal error')
@ -85,23 +114,8 @@ def color_distance_squared(c1, c2):
return dr * dr + dg * dg + db * db return dr * dr + dg * dg + db * db
class AnsiPixel: class AnsiPixel:
def __init__(self, pixel_u32): def __init__(self, pixel_u32, palette):
self.RGB99 = [ self.irc = self.nearest_hex_color(pixel_u32, palette)
0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2,
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
0x000047, 0x2e0047, 0x470047, 0x47002a, 0x740000, 0x743a00, 0x747400, 0x517400,
0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045,
0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5,
0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, 0xff0000, 0xff8c00, 0xffff00, 0xb2ff00,
0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098,
0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff,
0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c,
0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3,
0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f,
0xbcbcbc, 0xe2e2e2, 0xffffff
]
self.irc = self.nearest_hex_color(pixel_u32, self.RGB99)
def nearest_hex_color(self, pixel_u32, hex_colors): def nearest_hex_color(self, pixel_u32, hex_colors):
rgb_colors = [hex_to_rgb(color) for color in hex_colors] rgb_colors = [hex_to_rgb(color) for color in hex_colors]

View File

@ -84,7 +84,7 @@ class Bot():
self.loops = dict() self.loops = dict()
self.host = '' self.host = ''
self.playing = False 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.settings = {'flood':1, 'ignore':'big,birds,doc,gorf,hang,nazi,pokemon', 'lines':500, 'msg':0.03, 'palette':'RBG88', 'paste':True, 'png_width':80, 'results':25}
self.slow = False self.slow = False
self.reader = None self.reader = None
self.writer = None self.writer = None
@ -256,7 +256,7 @@ class Bot():
if url.startswith('https://') or url.startswith('http://'): if url.startswith('https://') or url.startswith('http://'):
try: try:
content = get_url(url).read() content = get_url(url).read()
ascii = await img2irc.convert(content, 512 - len(f":{identity.nickname}!{identity.username}@{self.host} PRIVMSG {chan} :\r\n"), 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']), self.settings['palette'])
except Exception as ex: except Exception as ex:
await self.irc_error(chan, 'failed to convert image', ex) await self.irc_error(chan, 'failed to convert image', ex)
else: else: