1
mirror of git://git.acid.vegas/scroll.git synced 2024-11-21 23:56:39 +00:00

Updated img2irc to use OpenCV instead of Pillow, random functions use a better seed for better randomization output, .ascii img moved into play function, etc

This commit is contained in:
Dionysus 2023-06-29 21:18:56 -04:00
parent ab69dc522b
commit f216fa3502
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
4 changed files with 135 additions and 135 deletions

View File

@ -9,7 +9,8 @@ Designed to be portable, there is no API key needed, no local art files needed,
## Dependencies ## Dependencies
* [python](https://www.python.org/) * [python](https://www.python.org/)
* [chardet](https://pypi.org/project/chardet/) *(`pip install chardet`)* * [chardet](https://pypi.org/project/chardet/) *(`pip install chardet`)*
* [pillow](https://pypi.org/project/pillow/) *(`pip install pillow`)* * [numpy](https://pypi.org/project/numpy/) *(`pip install numpy`)*
* [opencv-python](https://pypi.org/project/opencv-python/) *(`pip install opencv-python`)*
## Commands ## Commands
| Command | Description | | Command | Description |
@ -33,21 +34,17 @@ Designed to be portable, there is no API key needed, no local art files needed,
## Settings ## Settings
| Setting | Type | Description | | Setting | Type | Description |
| ---------------- | ------------ | -------------------------------------------------------------------------------------------- | | --------------------- | ------------ | -------------------------------------------------------------------------------------------- |
| `flood` | int or float | delay between each command | | `flood` | int or float | delay between each command |
| `ignore` | str | directories to ignore in `.ascii random` *(comma seperated list, no spaces)* | | `ignore` | str | directories to ignore in `.ascii random` *(comma seperated list, no spaces)* |
| `lines` | int | max lines outside of #scroll | | `lines` | int | max lines outside of #scroll |
| `msg` | int or float | delay between each message sent | | `msg` | int or float | delay between each message sent |
| `paste` | boolean | enable or disable `.ascii play` | | `paste` | boolean | enable or disable `.ascii play` |
| `png_brightness` | int or float | increase or decrease brightness for `.ascii img` output | | `png_quantize_colors` | int | quantize color option for `.ascii img` output |
| `png_contrast` | int or float | increase or decrease contrast for `.ascii img` output |
| `png_effect` | str | change the effect for `.ascii img` output *(blackwhite, blue, greyscale, invert, or smooth)* |
| `png_palette` | str | palette option for `.ascii img` output *(RGB99 or RGB88)* | | `png_palette` | str | palette option for `.ascii img` output *(RGB99 or RGB88)* |
| `png_width` | int | maximum width for `.ascii img` output | | `png_width` | int | maximum width for `.ascii img` output |
| `results` | int | max results to return in `.ascii search` | | `results` | int | max results to return in `.ascii search` |
**NOTE**: Setting **0** to `png_brightness`, `png_contrast`, or `png_effect` will disable the setting.
## Preview ## Preview
![](.screens/preview1.png) ![](.screens/preview1.png)
@ -59,7 +56,7 @@ Come pump with us in **#scroll** on [irc.supernets.org](ircs://irc.supernets.org
## Todo ## Todo
- git integration to `git clone` the [ircart](https://github.com/ircart/ircart) repository & `git pull` on `.ascii sync` *(Load art files into RAM for faster pumping)* - git integration to `git clone` the [ircart](https://github.com/ircart/ircart) repository & `git pull` on `.ascii sync` *(Load art files into RAM for faster pumping)*
- `.ascii scroll` command to loop playing random art files *(Stopped with `.ascii stop`)* - `.ascii scroll` command to loop playing random art files *(Stopped with `.ascii stop`)*
- Add arguments to `.ascii img` for contrast, brightness, * effects *(Take them out of self.settings)* - Add arguments to `.ascii img` for palette, width, & other options
- Setting to auto convert any image link to IRC art *(Emulate link previews like on Discord LOL)* - Setting to auto convert any image link to IRC art *(Emulate link previews like on Discord LOL)*
- `.ascii record` to record lines from the senders nick for uploads *(Stopped with `.ascii stop`)* - `.ascii record` to record lines from the senders nick for uploads *(Stopped with `.ascii stop`)*
- Improve randomness with `.ascii random` - Improve randomness with `.ascii random`

Binary file not shown.

View File

@ -2,28 +2,23 @@
# Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll) # Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll)
''' '''
Pull Request: Props:
- https://github.com/ircart/scroll/pull/3
Props:
- forked idea from malcom's img2irc (https://github.com/waveplate/img2irc) - forked idea from malcom's img2irc (https://github.com/waveplate/img2irc)
- big props to wrk (wr34k) for forking this one - big props to wrk (wr34k) for forking this + opencv implementation
- brightness/contrast/effects & more added by acidvegas
Interesting:
- https://pythonexamples.org/pillow-image-blend/
- https://pythonexamples.org/pillow-access-rgb-channels-of-image/
''' '''
import io
try: try:
from PIL import Image, ImageEnhance, ImageFilter, ImageOps import cv2
except ImportError: except ImportError:
raise SystemExit('missing required \'pillow\' library (https://pypi.org/project/pillow/)') raise SystemExit('missing required \'opencv-python\' library (https://pypi.org/project/opencv-python/)')
try:
import numpy as np
except ImportError:
raise SystemExit('missing required \'numpy\' library (https://pypi.org/project/numpy/)')
effects = ('blackwhite', 'blur', 'greyscale', 'invert', 'smooth')
palettes = { palettes = {
'RGB16': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x0, 0x0],
'RGB88': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00, 'RGB88': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x0, 0x0, 0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x0, 0x0,
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747, 0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
@ -35,7 +30,6 @@ palettes = {
0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff, 0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff,
0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c, 0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, 0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c,
0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3], 0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3],
'RGB99': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00, 'RGB99': [0xffffff, 0x000000, 0x00007f, 0x009300, 0xff0000, 0x7f0000, 0x9c009c, 0xfc7f00,
0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2, 0xffff00, 0x00fc00, 0x009393, 0x00ffff, 0x0000fc, 0xff00ff, 0x7f7f7f, 0xd2d2d2,
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747, 0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472c, 0x004747, 0x002747,
@ -51,50 +45,31 @@ palettes = {
0xbcbcbc, 0xe2e2e2, 0xffffff] 0xbcbcbc, 0xe2e2e2, 0xffffff]
} }
def convert(data, max_line_len, img_width=80, palette='RGB99', brightness=False, contrast=False, effect=None): def convert(data, max_line_len, img_width=80, palette='RGB99', quantize_colors=None):
if palette not in palettes: if palette not in palettes:
raise Exception('invalid palette option') raise Exception('invalid palette option')
if effect and effect not in effects:
raise Exception('invalid effect option')
palette = palettes[palette] palette = palettes[palette]
image = Image.open(io.BytesIO(data)) np_arr = np.asarray(bytearray(data), dtype="uint8")
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
del data del data
if brightness: return convert_image(image, quantize_colors, max_line_len, img_width, palette)
image = ImageEnhance.Brightness(im).enhance(brightness)
if contrast:
image = ImageEnhance.Contrast(image).enhance(contrast)
if effect == 'blackwhite':
image = image.convert("1")
elif effect == 'blur':
image - image.filter(ImageFilter.BLUR)
elif effect == 'greyscale':
image = image.convert("L")
elif effect == 'invert':
image = ImageOps.invert(image)
elif effect == 'smooth':
image = image.filter(ImageFilter.SMOOTH_MORE)
return convert_image(image, max_line_len, img_width, palette)
def convert_image(image, max_line_len, img_width, palette): def convert_image(orig_image, quantize_colors, max_line_len, img_width, palette):
(width, height) = image.size image = ircize(orig_image, img_width, quantize_colors)
img_height = img_width / width * height
del height, width
image.thumbnail((img_width, img_height), Image.Resampling.LANCZOS)
del img_height
CHAR = '\u2580' CHAR = '\u2580'
buf = list() buf = list()
for i in range(0, image.size[1], 2): for i in range(0, image.shape[0], 2):
if i+1 >= image.size[1]: if i+1 >= image.shape[0]:
bitmap = [[rgb_to_hex(image.getpixel((x, i))) for x in range(image.size[0])]] bitmap = [[bgr_to_hex(image[i, x]) for x in range(image.shape[1])]]
bitmap += [[0 for _ in range(image.size[0])]] bitmap += [[0 for _ in range(image.shape[1])]]
else: else:
bitmap = [[rgb_to_hex(image.getpixel((x, y))) for x in range(image.size[0])] for y in [i, i+1]] bitmap = [[bgr_to_hex(image[y, x]) for x in range(image.shape[1])] for y in [i, i+1]]
top_row = [AnsiPixel(px, palette) for px in bitmap[0]] top_row = [AnsiPixel(px, palette) for px in bitmap[0]]
bottom_row = [AnsiPixel(px, palette) 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()
for j in range(image.size[0]): for j in range(image.shape[1]):
top_pixel = top_row[j] top_pixel = top_row[j]
bottom_pixel = bottom_row[j] bottom_pixel = bottom_row[j]
pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel) pixel_pair = AnsiPixelPair(top_pixel, bottom_pixel)
@ -112,28 +87,52 @@ def convert_image(image, max_line_len, img_width, palette):
last_fg = fg last_fg = fg
last_bg = bg last_bg = bg
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 < 5:
raise Exception('internal error') raise Exception('image would get too small')
return convert_image(image, max_line_len, img_width-5, palette) return convert_image(orig_image, quantize_colors, max_line_len, img_width-5, palette)
return buf return buf
def ircize(image, img_width, quantize_colors):
(height, width, _) = image.shape
img_height = img_width / width * height
image = cv2.resize(image, (int(img_width), int(img_height)), interpolation=cv2.INTER_AREA)
brightness = np.sum(image) / (255 * image.shape[0] * image.shape[1])
minimum_brightness = 0.72
ratio = brightness / minimum_brightness
if ratio < 1:
image = cv2.convertScaleAbs(image, alpha = 1 / ratio, beta = 0)
imgf = np.float32(image).reshape(-1, 3)
criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,20,2.0)
compactness, label, center = cv2.kmeans(imgf, quantize_colors, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
final_img = center[label.flatten()]
image = final_img.reshape(image.shape)
return image
def hex_to_rgb(color): def hex_to_rgb(color):
r = color >> 16 r = (color >> 16) & 255
g = (color >> 8) % 256 g = (color >> 8) & 255
b = color % 256 b = color & 255
return (r,g,b) return (r,g,b)
def rgb_to_hex(rgb): def rgb_to_hex(rgb):
if len(list(rgb)) < 3:
r = g = b = rgb[0]
else:
r = rgb[0] r = rgb[0]
g = rgb[1] g = rgb[1]
b = rgb[2] b = rgb[2]
return (r << 16) + (g << 8) + b return (r << 16) + (g << 8) + b
def bgr_to_hex(bgr):
return rgb_to_hex((bgr[0], 0, 0)) if len(list(bgr)) < 3 else rgb_to_hex((bgr[2], bgr[1], bgr[0]))
def color_distance_squared(c1, c2): def color_distance_squared(c1, c2):
dr = c1[0] - c2[0] d1 = abs(c1[0] - c2[0])
dg = c1[1] - c2[1] d2 = abs(c1[1] - c2[1])
db = c1[2] - c2[2] d3 = abs(c1[2] - c2[2])
return dr * dr + dg * dg + db * db return d1 * d1 + d2 * d2 + d3 * d3
class AnsiPixel: class AnsiPixel:
def __init__(self, pixel_u32, palette): def __init__(self, pixel_u32, palette):

View File

@ -7,6 +7,7 @@ import json
import random import random
import re import re
import ssl import ssl
import sys
import time import time
import urllib.request import urllib.request
@ -15,7 +16,7 @@ class connection:
port = 6697 port = 6697
ipv6 = False ipv6 = False
ssl = True ssl = True
vhost = None vhost = None # Must in ('ip', port) format
channel = '#chats' channel = '#chats'
key = None key = None
modes = 'BdDg' modes = 'BdDg'
@ -90,10 +91,8 @@ class Bot():
'lines' : 500, 'lines' : 500,
'msg' : 0.03, 'msg' : 0.03,
'paste' : True, 'paste' : True,
'png_brightness' : 0,
'png_contrast' : 0,
'png_effect' : None,
'png_palette' : 'RGB99', 'png_palette' : 'RGB99',
'png_quantize_colors' : 99,
'png_width' : 80, 'png_width' : 80,
'results' : 25} 'results' : 25}
self.slow = False self.slow = False
@ -163,27 +162,47 @@ class Bot():
finally: finally:
self.db = cache self.db = cache
async def play(self, chan, name, paste=None):
try: try:
if paste: content = get_url(url).read()
except Exception as ex:
await self.irc_error(chan, 'failed to convert image', ex)
else:
if ascii:
if len(ascii) <= self.settings['lines']:
for line in ascii:
await self.sendmsg(chan, line)
await asyncio.sleep(self.settings['msg'])
else:
await self.irc_error('image is too big', 'take it to #scroll')
async def play(self, chan, name, img=False, paste=False):
try:
if img or paste:
ascii = get_url(name) ascii = get_url(name)
else: else:
ascii = get_url(f'https://raw.githubusercontent.com/ircart/ircart/master/ircart/{name}.txt') ascii = get_url(f'https://raw.githubusercontent.com/ircart/ircart/master/ircart/{name}.txt')
if ascii.getcode() == 200: if ascii.getcode() == 200:
if img:
ascii = img2irc.convert(ascii.read(), img, int(self.settings['png_width']), self.settings['png_palette'], int(self.settings['png_quantize_colors']))
else:
ascii = ascii.readlines() ascii = ascii.readlines()
if len(ascii) > int(self.settings['lines']) and chan != '#scroll': if len(ascii) > int(self.settings['lines']) and chan != '#scroll':
await self.irc_error(chan, 'file is too big', f'take those {len(ascii):,} lines to #scroll') await self.irc_error(chan, 'file is too big', f'take those {len(ascii):,} lines to #scroll')
else: else:
if not img and not paste:
await self.action(chan, 'the ascii gods have chosen... ' + color(name, cyan)) await self.action(chan, 'the ascii gods have chosen... ' + color(name, cyan))
for line in ascii: for line in ascii:
if type(line) == bytes:
try: try:
line = line.decode() line = line.decode()
except: except UnicodeError:
line = line.encode(chardet.detect(line)['encoding']).decode() # Get fucked UTF-16 line = line.decode(chardet.detect(line)['encoding']).encode().decode() # TODO: Do we need to re-encode/decode in UTF-8?
await self.sendmsg(chan, line.replace('\n','').replace('\r','') + reset) line = line.replace('\n','').replace('\r','')
await self.sendmsg(chan, line + reset)
await asyncio.sleep(self.settings['msg']) await asyncio.sleep(self.settings['msg'])
else: else:
await self.irc_error(chan, 'invalid name', name) await self.irc_error(chan, 'invalid name', name) if not img and not paste else await self.irc_error(chan, 'invalid url', name)
except Exception as ex: except Exception as ex:
try: try:
await self.irc_error(chan, 'error in play function', ex) await self.irc_error(chan, 'error in play function', ex)
@ -263,28 +282,25 @@ class Bot():
await asyncio.sleep(self.settings['msg']) await asyncio.sleep(self.settings['msg'])
elif args[1] == 'img' and len(args) == 3: elif args[1] == 'img' and len(args) == 3:
url = args[2] url = args[2]
width = 512 - len(line.split(' :')[0])+4
if url.startswith('https://') or url.startswith('http://'): if url.startswith('https://') or url.startswith('http://'):
try: self.playing = True
content = get_url(url).read() width = 512 - len(line.split(' :')[0])+4
ascii = img2irc.convert(content, 512 - len(f":{identity.nickname}!{identity.username}@{self.host} PRIVMSG {chan} :\r\n"), int(self.settings['png_width']), self.settings['png_palette'], self.settings['png_brightness'], self.settings['png_contrast'], self.settings['png_effect']) self.loops[chan] = asyncio.create_task(self.play(chan, url, img=width))
except Exception as ex:
await self.irc_error(chan, 'failed to convert image', ex)
else:
if ascii:
if len(ascii) <= self.settings['lines']:
for line in ascii:
await self.sendmsg(chan, line)
await asyncio.sleep(self.settings['msg'])
else:
await self.irc_error('image is too big', 'take it to #scroll')
elif msg == '.ascii list': elif msg == '.ascii list':
await self.sendmsg(chan, underline + color('https://raw.githubusercontent.com/ircart/ircart/master/ircart/.list', light_blue)) await self.sendmsg(chan, underline + color('https://raw.githubusercontent.com/ircart/ircart/master/ircart/.list', light_blue))
elif msg == '.ascii random': elif args[1] == 'random' and len(args) in (2,3):
self.playing = True if len(args) == 3:
dir = random.choice([item for item in self.db if item not in self.settings['ignore']]) dir = args[2]
else:
random.seed(random.randrange(sys.maxsize))
random.choice([item for item in self.db if item not in self.settings['ignore']])
if dir in self.db:
random.seed(random.randrange(sys.maxsize))
ascii = f'{dir}/{random.choice(self.db[dir])}' ascii = f'{dir}/{random.choice(self.db[dir])}'
self.playing = True
self.loops[chan] = asyncio.create_task(self.play(chan, ascii)) self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
else:
await self.irc_error(chan, 'invalid directory name', dir)
elif msg == '.ascii sync' and is_admin(ident): elif msg == '.ascii sync' and is_admin(ident):
await self.sync() await self.sync()
await self.sendmsg(chan, bold + color('database synced', light_green)) await self.sendmsg(chan, bold + color('database synced', light_green))
@ -294,14 +310,6 @@ class Bot():
self.loops[chan] = asyncio.create_task(self.play(chan, url, paste=True)) self.loops[chan] = asyncio.create_task(self.play(chan, url, paste=True))
else: else:
await self.irc_error(chan, 'invalid pastebin url', paste) await self.irc_error(chan, 'invalid pastebin url', paste)
elif args[1] == 'random' and len(args) == 3:
dir = args[2]
if dir in self.db:
self.playing = True
ascii = f'{dir}/{random.choice(self.db[dir])}'
self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
else:
await self.irc_error(chan, 'invalid directory name', dir)
elif args[1] == 'search' and len(args) == 3: elif args[1] == 'search' and len(args) == 3:
query = args[2] query = args[2]
results = [{'name':ascii,'dir':dir} for dir in self.db for ascii in self.db[dir] if query in ascii] results = [{'name':ascii,'dir':dir} for dir in self.db for ascii in self.db[dir] if query in ascii]
@ -322,7 +330,7 @@ class Bot():
setting = args[2] setting = args[2]
option = args[3] option = args[3]
if setting in self.settings: if setting in self.settings:
if setting in ('flood','lines','msg','png_brightness','png_contrast','png_width','results'): if setting in ('flood','lines','msg','png_quantize_colors','png_width','results'):
try: try:
option = float(option) option = float(option)
self.settings[setting] = option self.settings[setting] = option
@ -338,11 +346,6 @@ class Bot():
await self.sendmsg(chan, color('OK', light_green)) await self.sendmsg(chan, color('OK', light_green))
else: else:
await self.irc_error(chan, 'invalid option', 'must be on or off') await self.irc_error(chan, 'invalid option', 'must be on or off')
elif setting == 'png_effect' and option in ('false','none','off','0'):
self.settings[setting] = None
else:
self.settings[setting] = option
await self.sendmsg(chan, color('OK', light_green))
else: else:
await self.irc_error(chan, 'invalid setting', setting) await self.irc_error(chan, 'invalid setting', setting)
elif len(args) == 2: elif len(args) == 2:
@ -378,4 +381,5 @@ try:
import img2irc import img2irc
except ImportError: except ImportError:
raise SystemExit('missing required \'img2irc\' file (https://github.com/ircart/scroll/blob/master/img2irc.py)') raise SystemExit('missing required \'img2irc\' file (https://github.com/ircart/scroll/blob/master/img2irc.py)')
pass
asyncio.run(Bot().connect()) asyncio.run(Bot().connect())