ryuko-ng/robocop_ng/cogs/imagemanip.py

88 lines
2.7 KiB
Python
Raw Normal View History

2019-12-19 01:17:36 +00:00
import discord
from discord.ext import commands
from discord.ext.commands import Cog
2019-12-19 01:35:17 +00:00
from helpers.checks import check_if_staff_or_ot
2019-12-19 01:17:36 +00:00
import textwrap
import PIL.Image
import PIL.ImageFilter
import PIL.ImageOps
import PIL.ImageFont
import PIL.ImageDraw
class ImageManip(Cog):
def __init__(self, bot):
self.bot = bot
@commands.cooldown(1, 60 * 60 * 3, type=commands.BucketType.user)
2019-12-19 01:35:17 +00:00
@commands.check(check_if_staff_or_ot)
@commands.command(hidden=True)
2019-12-19 01:31:48 +00:00
async def cox(self, ctx, *, headline: str):
2019-12-19 01:17:36 +00:00
"""Gives a cox headline"""
mention = ctx.author.mention
headline = await commands.clean_content(fix_channel_mentions=True).convert(
ctx, headline
)
2020-04-05 20:50:58 +00:00
2019-12-19 01:17:36 +00:00
in_vice = "assets/motherboardlogo.png"
in_byjcox = "assets/byjcox.png"
font_path = "assets/neue-haas-grotesk-display-bold-regular.otf"
# Settings for image generation, don't touch anything
horipos = 18
vertpos = 75
line_spacing = 10
font_size = 50
2020-04-05 20:50:58 +00:00
image_width = 800
2019-12-19 01:17:36 +00:00
font_wrap_count = 30
sig_height = 15
# Wrap into lines
lines = textwrap.wrap(headline, width=font_wrap_count)
# not great, 4am be like
image_height = (len(lines) + 2) * (vertpos + line_spacing)
# Load font
f = PIL.ImageFont.truetype(font_path, font_size)
# Create image base, paste mobo logo
im = PIL.Image.new("RGB", (image_width, image_height), color="#FFFFFF")
moboim = PIL.Image.open(in_vice)
im.paste(moboim, (horipos, 17))
# Go through all the wrapped text lines
for line in lines:
# Get size of the text by font, create a new image of that size
size = f.getsize(line)
txt = PIL.Image.new("L", size)
2019-12-19 01:17:36 +00:00
# Draw the text
d = PIL.ImageDraw.Draw(txt)
d.text((0, 0), line, font=f, fill=255)
# Paste the text into the base image
w = txt.rotate(0, expand=1)
im.paste(
PIL.ImageOps.colorize(w, (0, 0, 0), (0, 0, 0)), (horipos, vertpos), w
)
2019-12-19 01:17:36 +00:00
# Calculate position on next line
vertpos += size[1] + line_spacing
# Add jcox signature
jcoxim = PIL.Image.open(in_byjcox)
im.paste(jcoxim, (horipos, vertpos + sig_height))
# Crop the image to the actual resulting size
im = im.crop((0, 0, image_width, vertpos + (sig_height * 3)))
# Save image
out_filename = f"/tmp/{ctx.message.id}-out.png"
im.save(out_filename, quality=100, optimize=True)
await ctx.send(content=f"{mention}: Enjoy.", file=discord.File(out_filename))
2019-12-19 01:17:36 +00:00
async def setup(bot):
await bot.add_cog(ImageManip(bot))