automeme/pillow_fight.py

84 lines
3.2 KiB
Python

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageOps
import requests
import io
from image_utils import ImageText
MARGIN_PERCENT = 0.2
LEFT_MARGIN_COLUMN = 20
CONTENT_COLUMN = 300
MIDDLE_MARGIN_COLUMN = 80
RIGHT_MARGIN_COLUMN = LEFT_MARGIN_COLUMN
TOP_MARGIN_ROW = 80
IMAGE_ROW = 300
MIDDLE_MARGIN_ROW = 20
TEXT_ROW = 300
BOTTOM_MARGIN_ROW = 80
total_image_size_x = 2*CONTENT_COLUMN + LEFT_MARGIN_COLUMN + RIGHT_MARGIN_COLUMN + MIDDLE_MARGIN_COLUMN
total_image_size_y = TOP_MARGIN_ROW + IMAGE_ROW + MIDDLE_MARGIN_ROW + TEXT_ROW + BOTTOM_MARGIN_ROW
def create_soy_vs_chad_meme(emoji1, emoji2, caption1, caption2):
left_image = get_emoji_from_rdrama(emoji1)
right_image = get_emoji_from_rdrama(emoji2)
#Resize images
left_image = ImageOps.contain(left_image, (CONTENT_COLUMN, IMAGE_ROW))
right_image = ImageOps.contain(right_image, (CONTENT_COLUMN, IMAGE_ROW))
left_image = ImageOps.mirror(left_image)
#Base image
base = Image.new(mode="RGB", size=(total_image_size_x,total_image_size_y), color=(255,255,255))
#Add images
base.paste(left_image, (LEFT_MARGIN_COLUMN,TOP_MARGIN_ROW), left_image)
base.paste(right_image, (LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN,TOP_MARGIN_ROW), right_image)
#Text regions
left_caption_box = ImageText((CONTENT_COLUMN, TEXT_ROW))
left_caption_box.fill_text_box((0,0), caption1, CONTENT_COLUMN, TEXT_ROW, font_filename="arial.ttf")
base.paste(left_caption_box.image, (LEFT_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW), left_caption_box.image)
right_caption_box = ImageText((CONTENT_COLUMN, TEXT_ROW))
right_caption_box.fill_text_box((0,0), caption2, CONTENT_COLUMN, TEXT_ROW, font_filename="arial.ttf")
base.paste(right_caption_box.image, (LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW), right_caption_box.image)
return base
def wrap_text_for_font(text, region_size, font_size) -> Image:
image = Image.new(mode="RGB", size=region_size, color=(255,255,255))
imageDraw = ImageDraw.ImageDraw()
imageDraw.multiline_text()
def get_emoji_from_rdrama(emoji_name):
cleaned_emoji_name : str = emoji_name
should_flip = False
if '!' in emoji_name:
cleaned_emoji_name = cleaned_emoji_name.replace("!", "")
should_flip = True
image = get_image_file_from_url(f"https://www.rdrama.net/e/{cleaned_emoji_name}.webp")
if should_flip:
image = ImageOps.mirror(image)
return image
def get_image_file_from_url(url):
r = requests.get(url)
image_file = io.BytesIO(r.content)
im = Image.open(image_file)
return im
#get_emoji_from_rdrama("zoomersoy").show()
create_soy_vs_chad_meme("!marseyrage", "marseyliathomas", "YOU RUINED MY LIFE!!!!", "I'm literally just existing").show()
# base_image = Image.new(mode="RGB", size=(1000,1000), color=(255,255,255))
# marppy_image = Image.open("marppy.webp")
# base_image.paste(marppy_image, (0,0), marppy_image)
# I1 = ImageDraw.Draw(base_image)
# font = ImageFont.truetype("impact.ttf", 65)
# I1.text((300, 0), "GO HOME GAMER GIRL", font=font, fill=(255, 0, 0))
# base_image.show()