Sarenrae: fix image manipulation

This commit is contained in:
skins 2021-09-01 21:32:17 +01:00
parent b9929e1c1c
commit f0e125b736

View File

@ -3,16 +3,19 @@ import discord
import requests import requests
import imgkit import imgkit
from PIL import Image, ImageOps from PIL import Image, ImageOps
import os import io
UID = 0x181eff7aacab91fa4051d1d995d44050 UID = 0x181eff7aacab91fa4051d1d995d44050
def image(name, html): def image(html):
open_image = imgkit.from_string(html, False) open_image = imgkit.from_string(html, False)
open_image = Image.open(open_image) open_image = Image.open(io.BytesIO(open_image))
image_box = ImageOps.invert(open_image.convert("RGB")).getbbox() image_box = ImageOps.invert(open_image.convert("RGB")).getbbox()
open_image.crop(image_box).save(f"{name}_cropped.png") file = io.BytesIO()
open_image.crop(image_box).save(file, "JPEG")
file.seek(0)
return file
class Sarenrae(commands.Cog): class Sarenrae(commands.Cog):
@ -30,11 +33,7 @@ class Sarenrae(commands.Cog):
headers = {"Authorization": await self.conf.token()} headers = {"Authorization": await self.conf.token()}
req = requests.get(f"https://api.pathfinder2.fr/v1/pf2/ancestry?name={name}", headers=headers).json() req = requests.get(f"https://api.pathfinder2.fr/v1/pf2/ancestry?name={name}", headers=headers).json()
html = req["results"][0]["data"]["description"]["value"] html = req["results"][0]["data"]["description"]["value"]
filename = f"ancestry-{name}" await ctx.send(file=discord.File(image(html)))
image(filename, html)
await ctx.send(file=discord.File(open(f"ancestry/{name}.png", "rb")))
os.remove(f"{filename}.png")
os.remove(f"{filename}_cropped.png")
@commands.command() @commands.command()
@checks.is_owner() @checks.is_owner()