110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
"""J.A.R.V.I.S. image processing cog."""
|
|
import re
|
|
from io import BytesIO
|
|
|
|
import aiohttp
|
|
import cv2
|
|
import numpy as np
|
|
from dis_snek import MessageContext, Scale, Snake, message_command
|
|
from dis_snek.models.discord.embed import EmbedField
|
|
from dis_snek.models.discord.file import File
|
|
from dis_snek.models.snek.command import cooldown
|
|
from dis_snek.models.snek.cooldowns import Buckets
|
|
|
|
from jarvis.utils import build_embed, convert_bytesize, unconvert_bytesize
|
|
|
|
MIN_ACCURACY = 0.80
|
|
|
|
|
|
class ImageCog(Scale):
|
|
"""
|
|
Image processing functions for J.A.R.V.I.S.
|
|
|
|
May be categorized under util later
|
|
"""
|
|
|
|
def __init__(self, bot: Snake):
|
|
self.bot = bot
|
|
self._session = aiohttp.ClientSession()
|
|
self.tgt_match = re.compile(r"([0-9]*\.?[0-9]*?) ?([KMGTP]?B)", re.IGNORECASE)
|
|
|
|
def __del__(self):
|
|
self._session.close()
|
|
|
|
async def _resize(self, ctx: MessageContext, target: str, url: str = None) -> None:
|
|
if not target:
|
|
await ctx.send("Missing target size, i.e. 200KB.")
|
|
return
|
|
|
|
tgt = self.tgt_match.match(target)
|
|
if not tgt:
|
|
await ctx.send(f"Invalid target format ({target}). Expected format like 200KB")
|
|
return
|
|
|
|
tgt_size = unconvert_bytesize(float(tgt.groups()[0]), tgt.groups()[1])
|
|
if tgt_size > unconvert_bytesize(8, "MB"):
|
|
await ctx.send("Target too large to send. Please make target < 8MB")
|
|
return
|
|
file = None
|
|
filename = None
|
|
if ctx.message.attachments is not None and len(ctx.message.attachments) > 0:
|
|
file = await ctx.message.attachments[0].read()
|
|
filename = ctx.message.attachments[0].filename
|
|
elif url is not None:
|
|
async with self._session.get(url) as resp:
|
|
if resp.status == 200:
|
|
file = await resp.read()
|
|
filename = url.split("/")[-1]
|
|
else:
|
|
ctx.send("Missing file as either attachment or URL.")
|
|
|
|
size = len(file)
|
|
if size <= tgt_size:
|
|
await ctx.send("Image already meets target.")
|
|
return
|
|
|
|
ratio = max(tgt_size / size - 0.02, 0.50)
|
|
|
|
accuracy = 0.0
|
|
|
|
while len(file) > tgt_size or (len(file) <= tgt_size and accuracy < MIN_ACCURACY):
|
|
old_file = file
|
|
|
|
buffer = np.frombuffer(file, dtype=np.uint8)
|
|
img = cv2.imdecode(buffer, flags=-1)
|
|
|
|
width = int(img.shape[1] * ratio)
|
|
height = int(img.shape[0] * ratio)
|
|
|
|
new_img = cv2.resize(img, (width, height))
|
|
file = cv2.imencode(".png", new_img)[1].tobytes()
|
|
accuracy = (len(file) / tgt_size) * 100
|
|
if accuracy <= 0.50:
|
|
file = old_file
|
|
ratio += 0.1
|
|
else:
|
|
ratio = max(tgt_size / len(file) - 0.02, 0.65)
|
|
|
|
bufio = BytesIO(file)
|
|
accuracy = (len(file) / tgt_size) * 100
|
|
fields = [
|
|
EmbedField("Original Size", convert_bytesize(size), False),
|
|
EmbedField("New Size", convert_bytesize(len(file)), False),
|
|
EmbedField("Accuracy", f"{accuracy:.02f}%", False),
|
|
]
|
|
embed = build_embed(title=filename, description="", fields=fields)
|
|
embed.set_image(url="attachment://resized.png")
|
|
await ctx.send(
|
|
embed=embed,
|
|
file=File(file=bufio, filename="resized.png"),
|
|
)
|
|
|
|
@message_command(name="resize")
|
|
@cooldown(bucket=Buckets.USER, rate=1, interval=60)
|
|
async def _resize_pref(self, ctx: MessageContext, target: str, url: str = None) -> None:
|
|
await self._resize(ctx, target, url)
|
|
|
|
|
|
def setup(bot: Snake) -> None:
|
|
"""Add ImageCog to J.A.R.V.I.S."""
|
|
ImageCog(bot)
|