aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/fun/latex.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/exts/fun/latex.py')
-rw-r--r--bot/exts/fun/latex.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/bot/exts/fun/latex.py b/bot/exts/fun/latex.py
index ac43e95b..80145843 100644
--- a/bot/exts/fun/latex.py
+++ b/bot/exts/fun/latex.py
@@ -1,10 +1,12 @@
import hashlib
import re
import string
+from io import BytesIO
from pathlib import Path
from typing import BinaryIO, Optional
import discord
+from PIL import Image
from discord.ext import commands
from bot.bot import Bot
@@ -27,6 +29,9 @@ CACHE_DIRECTORY = THIS_DIR / "_latex_cache"
CACHE_DIRECTORY.mkdir(exist_ok=True)
TEMPLATE = string.Template(Path("bot/resources/fun/latex_template.txt").read_text())
+BG_COLOR = (54, 57, 63, 255)
+PAD = 10
+
def _prepare_input(text: str) -> str:
if match := FORMATTED_CODE_REGEX.match(text):
@@ -35,6 +40,14 @@ def _prepare_input(text: str) -> str:
return text
+def _process_image(data: bytes, out_file: BinaryIO) -> None:
+ image = Image.open(BytesIO(data)).convert("RGBA")
+ width, height = image.size
+ background = Image.new("RGBA", (width + 2 * PAD, height + 2 * PAD), "WHITE")
+ background.paste(image, (PAD, PAD), image)
+ background.save(out_file)
+
+
class InvalidLatexError(Exception):
"""Represents an error caused by invalid latex."""
@@ -58,10 +71,9 @@ class Latex(commands.Cog):
raise InvalidLatexError(logs=response_json["log"])
async with self.bot.http_session.get(
f"{LATEX_API_URL}/{response_json['filename']}",
- data=payload,
raise_for_status=True
) as response:
- out_file.write(await response.read())
+ _process_image(await response.read(), out_file)
async def _upload_to_pastebin(self, text: str) -> Optional[str]:
"""Uploads `text` to the paste service, returning the url if successful."""