From f0e125b7366b44e1fadbf20c30354e1b5246fd23 Mon Sep 17 00:00:00 2001 From: skins Date: Wed, 1 Sep 2021 21:32:17 +0100 Subject: [PATCH] Sarenrae: fix image manipulation --- sarenrae/sarenrae.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sarenrae/sarenrae.py b/sarenrae/sarenrae.py index fecc3ff..0644711 100644 --- a/sarenrae/sarenrae.py +++ b/sarenrae/sarenrae.py @@ -3,16 +3,19 @@ import discord import requests import imgkit from PIL import Image, ImageOps -import os +import io UID = 0x181eff7aacab91fa4051d1d995d44050 -def image(name, html): +def image(html): 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() - 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): @@ -30,11 +33,7 @@ class Sarenrae(commands.Cog): headers = {"Authorization": await self.conf.token()} req = requests.get(f"https://api.pathfinder2.fr/v1/pf2/ancestry?name={name}", headers=headers).json() html = req["results"][0]["data"]["description"]["value"] - filename = f"ancestry-{name}" - 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") + await ctx.send(file=discord.File(image(html))) @commands.command() @checks.is_owner()