Last active
August 10, 2019 12:51
-
-
Save dittoslash/14bddb093a2149fcb439cafae454b13a to your computer and use it in GitHub Desktop.
cory in the png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
from PIL import Image | |
import os | |
#this entire program is just a lot of hardcoded numbers | |
client = discord.Client() | |
cory = Image.open("cory in the png.png") | |
cory1 = cory.crop((0, 0, 728, 399)) #upper bit of cory | |
cory2 = cory.crop((0, 399, 728, 399+73)) #middle bit of cory, to be resized | |
cory3 = cory.crop((0, 472, 728, 472+252)) #lower bit of cory | |
og_box = (220, 161) #og box size for cory's hands | |
og_paste = (75, 325, 75+220, 325+161) #og box location for cory's hands | |
og_aspect = og_box[0]/og_box[1] #og aspect ratio for og_box | |
@client.event | |
async def on_message(message): | |
if message.author.bot: return | |
if len(message.attachments) > 0: | |
await message.attachments[0].save("img.tmp") | |
image = Image.open("img.tmp", "r") | |
if image.width/image.height < og_aspect: | |
#step 1. resize the image so the width is 220, maintaining its aspect ratio. | |
resized_image = image.resize((220, int(220*image.height/image.width))) | |
#step 2. determine how big we need cory2 to be. at height 161, cory2 is 73 pixels high. | |
cory2_height = resized_image.height-88 | |
#step 3. determine image size, and create a new image at this size. | |
new_image_dims = (728, 399+cory2_height+252) | |
new_image = Image.new("RGBA", new_image_dims) | |
#step 4. paste in cory1, resize and paste cory2, paste in cory3. | |
new_image.paste(cory1, (0, 0, 728, 399)) | |
new_image.paste(cory2.resize((728, cory2_height)), (0, 399, 728, 399+cory2_height)) | |
new_image.paste(cory3, (0, 399+cory2_height, 728, 399+cory2_height+252)) | |
#step 5. paste in image. | |
new_paste = (75, 325, 75+220, 325+cory2_height+88) | |
new_image.paste(resized_image, new_paste) | |
#step 6. save | |
new_image.save("tmp.png") | |
else: | |
tcory = cory.copy() | |
tcory.paste(image.resize(og_box), og_paste) | |
tcory.save("tmp.png") | |
await message.channel.send(file=discord.File("tmp.png")) | |
os.remove("img.tmp") | |
os.remove("tmp.png") | |
await message.delete() | |
with open("TOKEN") as f: | |
client.run(f.readline()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment