diff --git a/pillow_fight.py b/pillow_fight.py index e9dac16..b5d899e 100644 --- a/pillow_fight.py +++ b/pillow_fight.py @@ -1,4 +1,5 @@ import math +from typing import Union from PIL import Image from PIL import ImageDraw from PIL import ImageFont @@ -115,14 +116,14 @@ class TwoCharacterWebcomicPanel(WebcomicPanel): left_text_region_y_size = int(panel_size_y/4) left_text_region_x_position = 0 left_text_region_y_position = 0 - add_text_box(base, self.left_caption, (left_text_region_x_size, left_text_region_y_size), (left_text_region_x_position,left_text_region_y_position), font=super().FONT) + add_text_box(base, self.left_caption, (left_text_region_x_size, left_text_region_y_size), (left_text_region_x_position,left_text_region_y_position), font=super().FONT, align="bl") # We put the text in the top half of the panel. right_text_region_x_size = int(CAPTION_UNITS*(panel_size_x/CAPTION_DIVISOR)) right_text_region_y_size = int(panel_size_y/4) right_text_region_x_position = int((CAPTION_DIVISOR-CAPTION_UNITS)*(panel_size_x/8)) right_text_region_y_position = int(panel_size_y/4) - add_text_box(base, self.right_caption, (right_text_region_x_size, right_text_region_y_size), (right_text_region_x_position,right_text_region_y_position), font=super().FONT) + add_text_box(base, self.right_caption, (right_text_region_x_size, right_text_region_y_size), (right_text_region_x_position,right_text_region_y_position), font=super().FONT, align="br") return add_border_to_image(base) @@ -150,7 +151,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): +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 = ""): if caption == "": return if init_font_size == 0: @@ -161,7 +162,24 @@ def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coo actual_text_box_size = line_image.write_text_box((0,0), caption, region_x_size, font_size=init_font_size, font_filename=font) actual_text_box_x_size, actual_text_box_y_size, input_text_block_x_size = actual_text_box_size if actual_text_box_y_size <= region_y_size: - base.paste(line_image.image, coordinates, line_image.image) + actual_paste_x_coordinates, actual_paste_y_coordinates = coordinates + if align != "": + # Horizontal Align + if "t" in align: + #This is the default + pass + elif "b" in align: + y_difference = region_y_size - actual_text_box_y_size + actual_paste_y_coordinates+=y_difference + + if "l" in align: + #This is the default + pass + elif "r" in align: + x_difference = region_x_size - actual_text_box_x_size + actual_paste_x_coordinates+=x_difference + + 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)