Resize images

master
HeyMoon 2022-09-04 14:52:55 -05:00
parent e26e7056cd
commit 3ba8fb6d50
1 changed files with 27 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import math
from typing import Tuple, Union
from typing import Callable, Tuple, Union
from PIL import Image
from PIL import ImageDraw, ImageSequence
from PIL import ImageFont
@ -78,7 +78,7 @@ def create_classic_meme(image: 'AnimatedImage', top_caption : str, bottom_captio
return image
def create_classic_meme_from_url(url : str, top_caption : str, bottom_caption : str) -> 'AnimatedImage':
return create_classic_meme(get_image_file_from_url(url), top_caption, bottom_caption)
return create_classic_meme(get_image_file_from_url(url).stretch_maintain_aspect_ratio(500), top_caption, bottom_caption)
def create_classic_meme_from_emoji(emoji : str, top_caption : str, bottom_caption : str):
EMOJI_SIZE = 600
@ -101,7 +101,7 @@ def create_modern_meme(image : 'AnimatedImage', caption : str) -> 'AnimatedImage
return base
def create_modern_meme_from_url(url : str, caption : str):
return create_modern_meme(get_image_file_from_url(url), caption)
return create_modern_meme(get_image_file_from_url(url).stretch_maintain_aspect_ratio(500), caption)
def create_modern_meme_from_emoji(emoji: str, caption: str):
EMOJI_SIZE = 600
@ -443,10 +443,31 @@ class AnimatedImage():
return AnimatedImage.from_image(base)
def fit(self, region : 'Tuple[int, int]') -> 'AnimatedImage':
'''
Shrinks the image while preserving it's aspect ratio, such that the new image has the same ratio but is contained in the given box.
'''
new_frames = []
for frame in self.frames:
new_frames.append(ImageOps.contain(frame, region))
return AnimatedImage(new_frames)
def stretch(self, region : 'Tuple[int, int]') -> 'AnimatedImage':
'''
Resizes the image such that the final image has the exact dimensions given by region
'''
return self.perform_transform_for_all_images(lambda image : image.resize(region, Image.ANTIALIAS))
def stretch_maintain_aspect_ratio(self, size : int) -> 'AnimatedImage':
aspect_ratio = self.height / self.width
ideal_height = int(size * aspect_ratio)
return self.stretch((size, ideal_height))
def perform_transform_for_all_images(self, transform : 'Callable[[Image.Image], Image.Image]') -> 'AnimatedImage':
new_frames = []
for frame in self.frames:
new_frames.append(transform(frame))
return AnimatedImage(new_frames)
def prime_decomposition(a):
primes = []
@ -504,8 +525,8 @@ def get_leftover_primes(a_, b_):
# TwoCharacterWebcomicPanel("marseytombstone", "", "!marseycry", "He had so much to live for"),
# OneCharacterWebcomicPanel("marseylaugh", "Just Kidding!", False)
# ]).save("webcomic.webp")
#create_modern_meme_from_url("https://media.giphy.com/media/gYkga3bZav66I/giphy.webp", "me when i see a black person (i am extremely racist)").save("racism.webp")
#create_classic_meme_from_url("https://rdrama.net/assets/images/rDrama/sidebar/98.webp", "in all seriousness it probably isn't worth the effort because this is just one of many ways they could goad the bot into saying something innapropriate. Like who cares lol its just one of many things they could say.", "also there isn't really any specific harm done to anyone by a bot saying pedophile nonsense, it's offensive at worst and funny at best. i laughed at least. also also this legit is something that only happens every 2500 comments or so (there was another comment a while back where bbbb said something similar)").save("bruh.webp")
#create_modern_meme_from_url("https://cdn.discordapp.com/attachments/1007776259910152292/1007833711540174885/unknown.png", "and then heymoon says the imposter is among us").save("modern_image.webp")
create_modern_meme_from_url("https://media.giphy.com/media/gYkga3bZav66I/giphy.webp", "me when i see a black person (i am extremely racist)").save("racism.webp")
#create_classic_meme_from_url("https://rdrama.net/images/16619117705921352.webp", "I left out 'hiking' intentionally since it's outdoor hobby 101. But even if he's trying to attract a homebody some kind of interest would be good to have. Like home brewing, art, instrument, or god forbid tabletop gaming.", "This just screams most boring, generic, codependent 'man' ever. I need a wife and I'll be happy to do whatever you want to do 24/7 because I have no personality, friends, or interests").save("classic.webp")
#create_modern_meme_from_url("https://rdrama.net/images/16617243111639555.webp", "It really does, especially with that weird lighting. ANd what's up with the subset of troons with Himmler eyes?").save("modern_image.webp")
#create_classic_meme_from_emoji("marseycock", "I WANT TO PUT MY DICK", "INSIDE OF MARSEY").save("classic_with_emoji.webp")
#create_modern_meme_from_emoji("rdramajanny", "youll be sorry when i get my mop you BITCH").save("modern_emoji.webp")