Literal bloat
This commit is contained in:
Ave Ozkal 2019-12-19 04:17:36 +03:00
parent f44e1ea17b
commit e4fcc81a13
No known key found for this signature in database
GPG key ID: 398DD7BD03276F6D
6 changed files with 85 additions and 0 deletions

View file

@ -61,6 +61,7 @@ initial_extensions = ['cogs.common',
'cogs.remind', 'cogs.remind',
'cogs.robocronp', 'cogs.robocronp',
'cogs.meme', 'cogs.meme',
'cogs.imagemanip',
'cogs.pin', 'cogs.pin',
'cogs.invites'] 'cogs.invites']

1
assets/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.otf

BIN
assets/byjcox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
assets/motherboardlogo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

80
cogs/imagemanip.py Normal file
View file

@ -0,0 +1,80 @@
import discord
from discord.ext import commands
from discord.ext.commands import Cog
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.command()
async def cox(self, ctx, *headline: str):
"""Gives a cox headline"""
mention = ctx.author.mention
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
image_width = 750
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)
# 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)
# 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))
def setup(bot):
bot.add_cog(ImageManip(bot))

View file

@ -5,3 +5,6 @@ humanize
parsedatetime parsedatetime
aiohttp aiohttp
gidgethub gidgethub
# Only if you'll enable imagemanip, which is only used for meme commands
Pillow