master
HeyMoon 2022-08-12 19:30:05 -05:00
parent 5afd9656cc
commit e5a6248600
2 changed files with 49 additions and 21 deletions

View File

@ -43,7 +43,7 @@ class ImageText(object):
text_size = self.get_text_size(font, font_size, text)
def write_text(self, xy, text, font_filename, font_size=11,
color=(0, 0, 0), max_width=None, max_height=None):
color=(0, 0, 0), max_width=None, max_height=None, stroke_color = None, stroke_size = 0):
x, y = xy
if isinstance(text, str):
try:
@ -60,7 +60,7 @@ class ImageText(object):
x = (self.size[0] - text_size[0]) / 2
if y == 'center':
y = (self.size[1] - text_size[1]) / 2
self.draw.text((x, y), text, font=font, fill=color)
self.draw.text((x, y), text, font=font, fill=color, stroke_width=stroke_size, stroke_fill=stroke_color)
return text_size
def get_text_size(self, font_filename, font_size, text):
@ -88,7 +88,7 @@ class ImageText(object):
def write_text_box(self, xy, text, box_width, font_filename,
font_size=11, color=(0, 0, 0), place='left',
justify_last_line=False):
justify_last_line=False, stroke_color = None, stroke_size = 0):
x, y = xy
lines = []
@ -113,23 +113,23 @@ class ImageText(object):
if place == 'left':
total_size = self.get_text_size(font_filename, font_size, line)
self.write_text((x, height), line, font_filename, font_size,
color)
color, stroke_color=stroke_color, stroke_size=stroke_size)
elif place == 'right':
total_size = self.get_text_size(font_filename, font_size, line)
x_left = x + box_width - total_size[0]
self.write_text((x_left, height), line, font_filename,
font_size, color)
font_size, color, stroke_color=stroke_color, stroke_size=stroke_size)
elif place == 'center':
total_size = self.get_text_size(font_filename, font_size, line)
x_left = int(x + ((box_width - total_size[0]) / 2))
self.write_text((x_left, height), line, font_filename,
font_size, color)
font_size, color, stroke_color=stroke_color, stroke_size=stroke_size)
elif place == 'justify':
words = line.split()
if (index == len(lines) - 1 and not justify_last_line) or \
len(words) == 1:
self.write_text((x, height), line, font_filename, font_size,
color)
color, stroke_color=stroke_color, stroke_size=stroke_size)
continue
line_without_spaces = ''.join(words)
total_size = self.get_text_size(font_filename, font_size,
@ -146,7 +146,7 @@ class ImageText(object):
words[-1])
last_word_x = x + box_width - last_word_size[0]
self.write_text((last_word_x, height), words[-1], font_filename,
font_size, color)
font_size, color, stroke_color=stroke_color, stroke_size=stroke_size)
height += text_height
if total_size[0] > max_x_size:
max_x_size = total_size[0]

View File

@ -9,6 +9,12 @@ import io
from image_utils import ImageText
from os.path import exists
TEST_MODE = True
class ColorScheme:
BLACK = 0
WHITE_WITH_BLACK_BORDER = 1
def create_soy_vs_chad_meme(emoji1, emoji2, caption1, caption2):
LEFT_MARGIN_COLUMN = 20
CONTENT_COLUMN = 300
@ -40,8 +46,22 @@ def create_soy_vs_chad_meme(emoji1, emoji2, caption1, caption2):
center_and_paste(base, right_image, (LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN,TOP_MARGIN_ROW), (CONTENT_COLUMN, IMAGE_ROW))
#Text regions
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))
add_text_box(base,
caption1,
(CONTENT_COLUMN, TEXT_ROW),
(LEFT_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW),
init_font_size=50,
align="cht",
font="impact.ttf",
color=ColorScheme.WHITE_WITH_BLACK_BORDER)
add_text_box(base,
caption2,
(CONTENT_COLUMN, TEXT_ROW),
(LEFT_MARGIN_COLUMN+CONTENT_COLUMN+MIDDLE_MARGIN_COLUMN, TOP_MARGIN_ROW+IMAGE_ROW+MIDDLE_MARGIN_ROW),
init_font_size=50,
align="cht",
font="impact.ttf",
color=ColorScheme.WHITE_WITH_BLACK_BORDER)
return add_watermark(base)
class WebcomicPanel():
@ -152,7 +172,7 @@ def create_webcomic(layout : 'list[WebcomicPanel]'):
image.paste(panel.create_image(), (x*assumed_panel_x_size, y*assumed_panel_y_size))
return add_watermark(image)
def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coordinates : tuple[int, int], font : str= "arial.ttf", init_font_size = 45, align :str = ""):
def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coordinates : tuple[int, int], font : str= "arial.ttf", init_font_size = 45, align :str = "", color = ColorScheme.BLACK):
if caption == "":
return
if init_font_size == 0:
@ -160,7 +180,15 @@ def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coo
return
region_x_size, region_y_size = region_size
line_image = ImageText((region_x_size, region_y_size))
actual_text_box_size = line_image.write_text_box((0,0), caption, region_x_size, font_size=init_font_size, font_filename=font)
if color == ColorScheme.BLACK:
fill_color = (0,0,0)
stroke = None
stroke_size = 0
if color == ColorScheme.WHITE_WITH_BLACK_BORDER:
fill_color = (255,255,255)
stroke = (0,0,0)
stroke_size = 3
actual_text_box_size = line_image.write_text_box((0,0), caption, region_x_size, font_size=init_font_size, font_filename=font, color=fill_color, stroke_color=stroke, stroke_size=stroke_size)
_, actual_text_box_y_size, input_text_block_x_size, actual_text_box_x_size = actual_text_box_size
if actual_text_box_y_size <= region_y_size:
actual_paste_x_coordinates, actual_paste_y_coordinates = coordinates
@ -174,9 +202,7 @@ def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coo
elif "b" in align:
actual_paste_y_coordinates+=y_difference
elif "cv" in align:
text_box_center_y_position = int(actual_text_box_y_size/2)
region_center_y_position = int(region_y_size/2)
actual_paste_y_coordinates = region_center_y_position - text_box_center_y_position
actual_paste_y_coordinates += int(y_difference/2)
if "l" in align:
#This is the default
@ -185,14 +211,16 @@ def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coo
actual_paste_x_coordinates+=x_difference
elif "ch" in align:
text_box_center_x_position = int(actual_text_box_x_size/2)
region_center_x_position = int(region_x_size/2)
actual_paste_x_coordinates = region_center_x_position - text_box_center_x_position
image_draw = ImageDraw.Draw(base)
image_draw.rectangle(((actual_paste_x_coordinates, actual_paste_y_coordinates), (actual_paste_x_coordinates+actual_text_box_x_size, actual_text_box_y_size+actual_paste_y_coordinates)), fill="tan")
actual_paste_x_coordinates += int(x_difference/2)
if TEST_MODE:
image_draw = ImageDraw.Draw(base)
image_draw.rectangle((coordinates, (coordinates[0]+region_size[0], coordinates[1]+region_size[1])), fill="blue")
image_draw.rectangle(((actual_paste_x_coordinates, actual_paste_y_coordinates), (actual_paste_x_coordinates+actual_text_box_x_size, actual_text_box_y_size+actual_paste_y_coordinates)), fill="tan")
base.paste(line_image.image, (actual_paste_x_coordinates, actual_paste_y_coordinates), line_image.image)
else:
add_text_box(base, caption, region_size, coordinates, font=font, init_font_size=init_font_size-1)
add_text_box(base, caption, region_size, coordinates, font=font, init_font_size=init_font_size-1, align=align, color=color)
def add_text(base : Image, caption : str, region_size : tuple[int, int], coordinates : tuple[int, int], font : str= "arial.ttf"):
if caption == "":