From 5afd9656cca794d82d44942fcafeb5169a844ab0 Mon Sep 17 00:00:00 2001 From: HeyMoon <101842728+UnironicHeyMoon@users.noreply.github.com> Date: Fri, 12 Aug 2022 18:13:18 -0500 Subject: [PATCH] centering and such --- .gitignore | 2 ++ image_utils.py | 7 ++++-- pillow_fight.py | 60 +++++++++++++++++++++++++++---------------------- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 7a275ac..c660c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ __pycache__/image_utils.cpython-39.pyc +emoji_cache/ +*.ttf \ No newline at end of file diff --git a/image_utils.py b/image_utils.py index 495f481..e60b50a 100644 --- a/image_utils.py +++ b/image_utils.py @@ -72,7 +72,7 @@ class ImageText(object): justify_last_line=False): font_size = start_font_size while True: - width, height, box_width = self.write_text_box(xy, text, box_width, + width, height, box_width, _ = self.write_text_box(xy, text, box_width, font_filename, font_size, color, place) if (width >= box_width) or (height >= box_height): @@ -108,6 +108,7 @@ class ImageText(object): lines = [' '.join(line) for line in lines if line] height = y + max_x_size = -1 for index, line in enumerate(lines): if place == 'left': total_size = self.get_text_size(font_filename, font_size, line) @@ -147,4 +148,6 @@ class ImageText(object): self.write_text((last_word_x, height), words[-1], font_filename, font_size, color) height += text_height - return (total_size[0], height - y, box_width) \ No newline at end of file + if total_size[0] > max_x_size: + max_x_size = total_size[0] + return (total_size[0], height - y, box_width, max_x_size) \ No newline at end of file diff --git a/pillow_fight.py b/pillow_fight.py index b5d899e..2d856de 100644 --- a/pillow_fight.py +++ b/pillow_fight.py @@ -55,27 +55,28 @@ class WebcomicPanel(): return Image.new(mode="RGB", size=(self.PANEL_SIZE,self.PANEL_SIZE), color=(255,255,255)) class OneCharacterWebcomicPanel(WebcomicPanel): - def __init__(self, emoji, caption): + def __init__(self, emoji, caption, words_in_background): self.emoji = emoji self.caption = caption + self.words_in_background = words_in_background def create_image(self) -> Image: base = super().create_image() panel_size_x, panel_size_y = base.size + # We put the text in the background of the panel. + text_region_x_size = int(panel_size_x) + text_region_y_size = int(panel_size_y if self.words_in_background else panel_size_y/2) + add_text(base, self.caption, (text_region_x_size, text_region_y_size), (0,0), font=super().FONT) + # We put marsey in the bottom left quadrant - emoji_region_x_size = int(panel_size_x/2) + emoji_region_x_size = int(panel_size_x) emoji_region_y_size = int(panel_size_y/2) emoji_placement_position_x = 0 emoji_placement_position_y = int(panel_size_y/2) emoji = get_emoji_from_rdrama(self.emoji) center_and_paste(base, emoji, (emoji_placement_position_x, emoji_placement_position_y), (emoji_region_x_size, emoji_region_y_size)) - # We put the text in the top half of the panel. - text_region_x_size = int(panel_size_x) - text_region_y_size = int(panel_size_y/2) - add_text(base, self.caption, (text_region_x_size, text_region_y_size), (0,0), font=super().FONT) - return add_border_to_image(base) class TwoCharacterWebcomicPanel(WebcomicPanel): @@ -134,7 +135,7 @@ class TitleCardWebcomicPanel(WebcomicPanel): def create_image(self) -> Image: base = super().create_image() - add_text(base, self.caption, base.size, (0,0), font=super().FONT) + add_text_box(base, self.caption, base.size, (0,0), font=super().FONT, init_font_size=90, align="cvch") return add_border_to_image(base) @@ -160,25 +161,35 @@ def add_text_box(base : Image, caption : str, region_size : tuple[int, int], coo 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) - actual_text_box_x_size, actual_text_box_y_size, input_text_block_x_size = actual_text_box_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 if align != "": + y_difference = region_y_size - actual_text_box_y_size + x_difference = region_x_size - actual_text_box_x_size # 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 - + 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 + 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 - + 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") 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) @@ -227,11 +238,6 @@ def center_and_paste(base : Image, to_paste : Image, coordinates: tuple[int, int base.paste(to_paste, (x, y), to_paste) def add_border_to_image(image : Image, thickness : int = 5): - # image_draw = ImageDraw.Draw(image) - # image_x_size, image_y_size = image.size - # inner_rectangle_x_size, inner_rectangle_y_size = image_x_size - 2*thickness, image_y_size - 2*thickness - # image_draw.rectangle([(0,0),(image_x_size, image_y_size)], fill="black") - # image_draw.rectangle([(thickness, thickness),(image_x_size-thickness, image_y_size-thickness)], fill="white") inner_image_x_size, inner_image_y_size = image.size outer_image_x_size, outer_image_y_size = inner_image_x_size + 2*thickness, inner_image_y_size + 2*thickness outside_image = Image.new(mode="RGB", size=(outer_image_x_size,outer_image_y_size), color=(0,0,0)) @@ -266,13 +272,13 @@ def get_image_file_from_url(url): im = Image.open(image_file) return im -#create_soy_vs_chad_meme("bigsmilesoyjak", "!marseyshooting", "I have fun new toys and games for your children", "Die").show() +create_soy_vs_chad_meme("bigsmilesoyjak", "!marseyshooting", "I have fun new toys and games for your children", "Die").show() -create_webcomic([ - TwoCharacterWebcomicPanel("soyjak", "Black people deserve the rope", "marseyconfused", "Why?"), - TwoCharacterWebcomicPanel("seethejak", "Because they are degenerate!!!", "marseysmug", "Kinda like you?"), - OneCharacterWebcomicPanel("soycry", "No!! I am a pure aryan white boy and I am special Hitler told me so, so fuck off you nigger cunt. God I hate you so much, if I commit suicide its gonna be your fault, fuck you"), - TitleCardWebcomicPanel("One Hour Later..."), - TwoCharacterWebcomicPanel("marseytombstone", "", "!marseycry", "He had so much to live for"), - OneCharacterWebcomicPanel("marseylaugh", "Just Kidding!") -]).show() \ No newline at end of file +# create_webcomic([ +# TwoCharacterWebcomicPanel("soyjak", "Black people deserve the rope", "marseyconfused", "Why?"), +# TwoCharacterWebcomicPanel("seethejak", "Because they are degenerate!!!", "marseysmug", "Kinda like you?"), +# OneCharacterWebcomicPanel("soycry", "No!! I am a pure aryan white boy and I am special Hitler told me so, so fuck off you nigger cunt. God I hate you so much, if I commit suicide its gonna be your fault, fuck you, its not fair, why do you always have to bully me around, you are such a bitch, i hate you so much, hitler was right and six million wasnt nearly enough, the jews are responsible for this and they will be punished on the day of the rope, youll be sorry then, plus i am armed and dangerous and i have a gun and i will shoot you if you keep making fun of me plus youll be in troulbe if I kill myself so how do you like that you stupid nigger bitch. fuck", words_in_background=True), +# TitleCardWebcomicPanel("One Hour Later..."), +# TwoCharacterWebcomicPanel("marseytombstone", "", "!marseycry", "He had so much to live for"), +# OneCharacterWebcomicPanel("marseylaugh", "Just Kidding!", False) +# ]).show() \ No newline at end of file