Back

Draw a text in center of an Image


Press Create Image if you want to create an image with the text you wrote in the center of it. Please, fill you parameters according to your preferences.

The code for center the text inside the image have been written in python using PIL library. The code is:

from PIL import Image, ImageDraw, ImageFont, ImageFilter


filter_dict = {
    'BLUR' : ImageFilter.BLUR,
    'CONTOUR' : ImageFilter.CONTOUR,
    'DETAIL' : ImageFilter.DETAIL,
    'EDGE_ENHANCE' : ImageFilter.EDGE_ENHANCE,
    'EDGE_ENHANCE_MORE' : ImageFilter.EDGE_ENHANCE_MORE,
    'EMBOSS' : ImageFilter.EMBOSS,
    'FIND_EDGES' : ImageFilter.FIND_EDGES,
    'SMOOTH' : ImageFilter.SMOOTH,
    'SMOOTH_MORE' : ImageFilter.SMOOTH_MORE,
    'SHARPEN' : ImageFilter.SHARPEN
}

def create_image(font_name, font_size, font_color, width, height, back_ground_color, text, image_filter=None):

    font  =  ImageFont.truetype ( font_path + font_name + ".ttf", font_size )

    im  =  Image.new ( "RGB", (width,height), back_ground_color )
    draw  =  ImageDraw.Draw ( im )
    text_x, text_y = font.getsize(text)
    x = (width - text_x)/2
    y = (height - text_y)/2
    draw.text ( (x,y), text, font=font, fill=font_color )

    if filter:
        real_filter = filter_dict[image_filter]
        im = im.filter(real_filter)
    im.save ( "runaway_emboss.jpg" )