centering, watermarks and such
parent
e6dbb1a6f7
commit
25836cdbf8
|
@ -6,7 +6,6 @@ import requests
|
|||
import io
|
||||
from image_utils import ImageText
|
||||
|
||||
MARGIN_PERCENT = 0.2
|
||||
LEFT_MARGIN_COLUMN = 20
|
||||
CONTENT_COLUMN = 300
|
||||
MIDDLE_MARGIN_COLUMN = 80
|
||||
|
@ -14,14 +13,12 @@ RIGHT_MARGIN_COLUMN = LEFT_MARGIN_COLUMN
|
|||
TOP_MARGIN_ROW = 80
|
||||
IMAGE_ROW = 300
|
||||
MIDDLE_MARGIN_ROW = 20
|
||||
TEXT_ROW = 300
|
||||
BOTTOM_MARGIN_ROW = 80
|
||||
TEXT_ROW = 100
|
||||
BOTTOM_MARGIN_ROW = 200
|
||||
|
||||
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)
|
||||
|
@ -30,25 +27,59 @@ def create_soy_vs_chad_meme(emoji1, emoji2, caption1, caption2):
|
|||
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)
|
||||
right_image = ImageOps.mirror(right_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)
|
||||
center_and_paste(base, left_image, (LEFT_MARGIN_COLUMN,TOP_MARGIN_ROW), (CONTENT_COLUMN, IMAGE_ROW))
|
||||
center_and_paste(base, right_image, (LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN,TOP_MARGIN_ROW), (CONTENT_COLUMN, IMAGE_ROW))
|
||||
|
||||
#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)
|
||||
add_text(base, caption1, (CONTENT_COLUMN, TEXT_ROW), (LEFT_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW))
|
||||
add_text(base, caption2, (CONTENT_COLUMN, TEXT_ROW), (LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW))
|
||||
return add_watermark(base)
|
||||
|
||||
def add_text(base : Image, caption : str, region_size : tuple[int, int], coordinates : tuple[int, int], font : str= "arial.ttf"):
|
||||
region_x_size, region_y_size = region_size
|
||||
line_image = ImageText((region_x_size, region_y_size))
|
||||
line_image.fill_text_box((0,0), caption, region_x_size, region_y_size, font_filename=font)
|
||||
base.paste(line_image.image, coordinates, line_image.image)
|
||||
|
||||
def add_watermark(image : Image):
|
||||
WATERMARK_HEIGHT = 30
|
||||
image_size_x, image_size_y = image.size
|
||||
base = Image.new(mode="RGB", size=(image_size_x, image_size_y + WATERMARK_HEIGHT), color=(255,255,255))
|
||||
base.paste(image)
|
||||
|
||||
marsey = get_emoji_from_rdrama("marseybangfast")
|
||||
marsey = ImageOps.contain(marsey, (WATERMARK_HEIGHT, WATERMARK_HEIGHT))
|
||||
base.paste(marsey, (0, image_size_y), marsey)
|
||||
|
||||
text_line_size = int(WATERMARK_HEIGHT/2)
|
||||
|
||||
add_text(base, "A meme by HeyMoon and Foo", (image_size_x, text_line_size), (WATERMARK_HEIGHT, image_size_y))
|
||||
add_text(base, "For instructions on how to legally build a pipe bomb, go to rdrama.net", (image_size_x, text_line_size), (WATERMARK_HEIGHT, image_size_y+text_line_size))
|
||||
|
||||
return base
|
||||
|
||||
def center_and_paste(base : Image, to_paste : Image, coordinates: tuple[int, int], box_size : tuple[int, int]):
|
||||
image_size_x, image_size_y = to_paste.size
|
||||
box_size_x, box_size_y = box_size
|
||||
|
||||
extra_space_x = box_size_x - image_size_x
|
||||
extra_space_y = box_size_y - image_size_y
|
||||
|
||||
offset_x = int(extra_space_x/2)
|
||||
offset_y = int(extra_space_y/2)
|
||||
|
||||
box_coordinate_x, box_coordinate_y = coordinates
|
||||
|
||||
x, y = offset_x + box_coordinate_x, offset_y + box_coordinate_y
|
||||
|
||||
base.paste(to_paste, (x, y), to_paste)
|
||||
|
||||
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()
|
||||
|
@ -73,7 +104,7 @@ def get_image_file_from_url(url):
|
|||
|
||||
#get_emoji_from_rdrama("zoomersoy").show()
|
||||
|
||||
create_soy_vs_chad_meme("!marseyrage", "marseyliathomas", "YOU RUINED MY LIFE!!!!", "I'm literally just existing").show()
|
||||
create_soy_vs_chad_meme("bigsmilesoyjak", "!marseyshooting", "I have fun new toys and games for your children", "Die").show()
|
||||
|
||||
# base_image = Image.new(mode="RGB", size=(1000,1000), color=(255,255,255))
|
||||
# marppy_image = Image.open("marppy.webp")
|
||||
|
|
Loading…
Reference in New Issue