mentalsite.blogg.se

Meme creator screenshot 1
Meme creator screenshot 1







wrap ( text ) def get_font ( size ): path = os. TextWrapper ( width = wrap_width ) return wrapper. size )) return expand_canvas def wrap_text ( text, wrap_width ): wrapper = textwrap. paste ( image, ( 0, 0 )) expand_canvas. new ( 'RGB', ( width, height ), background_color ) expand_canvas. text (( left, row_height ), line, fill = text_color, font = font ) return image def bottom_expand_image_with_image ( image, expand_image, background_color ): width = image. textsize ( line, font = font ) left = ( image_width - line_width ) / 2 draw_canvas. Draw ( image ) for row, line in enumerate ( text_lines ): row_height = row * text_height line_width, _ = draw_canvas. new ( 'RGB', ( image_width, total_text_height ), background_color ) draw_canvas = PIL. textsize ( text, font = font ) if text_width > image_width : character_width = text_width / len ( text ) max_characters_count = int ( image_width / character_width ) text_lines = wrap_text ( text, wrap_width = max_characters_count ) else : text_lines = total_text_height = len ( text_lines ) * text_height image = PIL. Draw ( placeholder ) text_width, text_height = draw_canvas. new ( 'RGB', ( 0, 0 ), background_color ) font = get_font ( size = text_size ) draw_canvas = PIL. paste ( image, ( border_size, border_size )) return border_canvas def get_text_as_image ( text, text_color, text_size, image_width, background_color ): placeholder = PIL. new ( 'RGB', ( width, height ), border_color ) border_canvas. size width = original_width + border_size * 2 height = original_height + border_size * 2 border_canvas = PIL. Import os import textwrap import PIL.Image import PIL.ImageDraw import PIL.ImageFont def draw_border ( image, border_size, border_color ): original_width, original_height = image. Then we have to combine the original image and text image at the bottom of it with this helper function: We return the image with text while not doing anything on the original image itself. text (( 0, row_height ), line, fill = text_color, font = font ) return imageĪt first we just generate a placeholder image to get text width and height, then we get the text lines as previously but in the end, we create a new image of the required size and draw text on it. Draw ( image ) for row, line in enumerate ( text_lines ): row_height = row * text_height draw_canvas. So we refactor our draw_text function into get_text_as_image function:ĭef get_text_as_image ( text, text_color, text_size, image_width, background_color ): placeholder = PIL.

meme creator screenshot 1

This way text generation doesn't have to take into account coordinates on the original image, which simplifies things. So we create an image with text written on it, get the original image, expand it by text height and paste the text image below it. This is getting a bit complex for one function so let us do some refactors first.Ī good idea when handling complex operations with Pillow is to extract some operations into their own images. The flow is to get the total height all text lines would take and then expand the image at the bottom similar to how we expanded the image to draw a border. Now how do we get the text below the image like this:

#Meme creator screenshot 1 code#

You may want to alter this part of the code a bit like taking 90-80% of the max_characters_count so that so even the worst-case scenario will break into lines properly.

meme creator screenshot 1

The max_characters_count is an estimate as some characters will take more pixels than others. We will take care of that in a moment but few things to note here. So the first line would draw at 0,0 and the second line would draw at 0,some_height, and so on:Īs you can see we wrapped the text but it's still being drawn in the top left corner of the image which we don't want. We enumerate each line to then draw it at the correct Y-axis position (lower and lower). text (( 0, row * text_height ), line, fill = text_color, font = font ) For row, line in enumerate ( text_lines ): draw_canvas.







Meme creator screenshot 1