Migrate image, closes #97
This commit is contained in:
parent
c69c21e9d4
commit
a23c28e551
1 changed files with 16 additions and 14 deletions
|
@ -5,21 +5,23 @@ from io import BytesIO
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from discord import File
|
from dis_snek import MessageContext, Scale, Snake, message_command
|
||||||
from discord.ext import commands
|
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
|
from jarvis.utils import build_embed, convert_bytesize, unconvert_bytesize
|
||||||
from jarvis.utils.field import Field
|
|
||||||
|
|
||||||
|
|
||||||
class ImageCog(commands.Cog):
|
class ImageCog(Scale):
|
||||||
"""
|
"""
|
||||||
Image processing functions for J.A.R.V.I.S.
|
Image processing functions for J.A.R.V.I.S.
|
||||||
|
|
||||||
May be categorized under util later
|
May be categorized under util later
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: Snake):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self._session = aiohttp.ClientSession()
|
self._session = aiohttp.ClientSession()
|
||||||
self.tgt_match = re.compile(r"([0-9]*\.?[0-9]*?) ?([KMGTP]?B)", re.IGNORECASE)
|
self.tgt_match = re.compile(r"([0-9]*\.?[0-9]*?) ?([KMGTP]?B)", re.IGNORECASE)
|
||||||
|
@ -27,7 +29,7 @@ class ImageCog(commands.Cog):
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self._session.close()
|
self._session.close()
|
||||||
|
|
||||||
async def _resize(self, ctx: commands.Context, target: str, url: str = None) -> None:
|
async def _resize(self, ctx: MessageContext, target: str, url: str = None) -> None:
|
||||||
if not target:
|
if not target:
|
||||||
await ctx.send("Missing target size, i.e. 200KB.")
|
await ctx.send("Missing target size, i.e. 200KB.")
|
||||||
return
|
return
|
||||||
|
@ -84,23 +86,23 @@ class ImageCog(commands.Cog):
|
||||||
bufio = BytesIO(file)
|
bufio = BytesIO(file)
|
||||||
accuracy = (len(file) / tgt_size) * 100
|
accuracy = (len(file) / tgt_size) * 100
|
||||||
fields = [
|
fields = [
|
||||||
Field("Original Size", convert_bytesize(size), False),
|
EmbedField("Original Size", convert_bytesize(size), False),
|
||||||
Field("New Size", convert_bytesize(len(file)), False),
|
EmbedField("New Size", convert_bytesize(len(file)), False),
|
||||||
Field("Accuracy", f"{accuracy:.02f}%", False),
|
EmbedField("Accuracy", f"{accuracy:.02f}%", False),
|
||||||
]
|
]
|
||||||
embed = build_embed(title=filename, description="", fields=fields)
|
embed = build_embed(title=filename, description="", fields=fields)
|
||||||
embed.set_image(url="attachment://resized.png")
|
embed.set_image(url="attachment://resized.png")
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
embed=embed,
|
embed=embed,
|
||||||
file=File(bufio, filename="resized.png"),
|
file=File(file=bufio, filename="resized.png"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@commands.command(name="resize", help="Resize an image")
|
@message_command(name="resize")
|
||||||
@commands.cooldown(1, 60, commands.BucketType.user)
|
@cooldown(bucket=Buckets.USER, rate=1, interval=60)
|
||||||
async def _resize_pref(self, ctx: commands.Context, target: str, url: str = None) -> None:
|
async def _resize_pref(self, ctx: MessageContext, target: str, url: str = None) -> None:
|
||||||
await self._resize(ctx, target, url)
|
await self._resize(ctx, target, url)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot: commands.Bot) -> None:
|
def setup(bot: Snake) -> None:
|
||||||
"""Add ImageCog to J.A.R.V.I.S."""
|
"""Add ImageCog to J.A.R.V.I.S."""
|
||||||
bot.add_cog(ImageCog(bot))
|
bot.add_cog(ImageCog(bot))
|
||||||
|
|
Loading…
Add table
Reference in a new issue