Example #1
0
def draw_text(image, text, horizontal_offset, vertical_offset,
              horizontal_justification, vertical_justification, size,
              color='#FFFFFF', orientation=None, font=None):
    """Draws text on an image."""
    image = convert_safe_mode(image)
    if orientation:
        orientation = getattr(Image, orientation)

    draw = ImageDraw.Draw(image)
    if font.strip():
        font = ImageFont.truetype(font, size)
    else:
        font = ImageFont.load_default()
        text = text.encode('ascii', 'replace')

    if orientation:
        font = ImageFont.TransposedFont(font, orientation)

    location = calculate_location(
        horizontal_offset, vertical_offset,
        horizontal_justification, vertical_justification,
        image.size, draw.textsize(text, font=font))

    # draw
    draw.text(location, text, font=font, fill=color)

    # composite the watermark with the layer
    return image
Example #2
0
def draw_text(image,
              text,
              horizontal_offset,
              vertical_offset,
              horizontal_justification,
              vertical_justification,
              size,
              color='#FFFFFF',
              orientation=None,
              font=None):
    """Draws text on an image."""
    image = convert_safe_mode(image)
    if orientation:
        orientation = getattr(Image, orientation)

    draw = ImageDraw.Draw(image)
    if font.strip():
        font = ImageFont.truetype(font, size)
    else:
        font = ImageFont.load_default()
        text = text.encode('ascii', 'replace')

    if orientation:
        font = ImageFont.TransposedFont(font, orientation)

    location = calculate_location(horizontal_offset, vertical_offset,
                                  horizontal_justification,
                                  vertical_justification, image.size,
                                  draw.textsize(text, font=font))

    # draw
    draw.text(location, text, font=font, fill=color)

    # composite the watermark with the layer
    return image
def draw_text(image, text, horizontal_offset, vertical_offset,
              horizontal_justification, vertical_justification, size, opacity, hallo,
              cache = {}, color='#FFFFFF', orientation=None, font=None):
    """Draws text on an image."""
    image = convert_safe_mode(image)
    img_size = image.size

    mask_layer = Image.new('L',img_size, '#FFFFFF')
    color_layer = Image.new('RGB',img_size, color)

    draw = ImageDraw.Draw(mask_layer)

    if orientation:
        orientation = getattr(Image, orientation)

    if font.strip():
        font = ImageFont.truetype(font, size)
    else:
        font = ImageFont.load_default()
        text = text.encode('ascii', 'replace')

    if orientation:
        font = ImageFont.TransposedFont(font, orientation)

    location = calculate_location(
        horizontal_offset, vertical_offset,
        horizontal_justification, vertical_justification,
        image.size, draw.textsize(text, font=font))

    if hallo:
        x,y = img_size
        blurred_id = BLURED_ID % (x,y)
        if blurred_id in cache:
			hallo_mask_layer = cache[blurred_id]
        else:
            hallo_mask_layer = Image.new('L',img_size, '#FFFFFF')
            draw_hallo = ImageDraw.Draw(hallo_mask_layer)
            draw_hallo.text(location, text, font=font, fill=255-opacity)
            n = 0
            while n < size/10:
            	hallo_mask_layer = hallo_mask_layer.filter(ImageFilter.BLUR)
            	draw_hallo.text(location, text, font=font, fill=255-opacity)
            	n += 1
            cache[blurred_id] = hallo_mask_layer
		
        hallo_color_layer = Image.new('RGB',img_size, color)
        hallo_color_layer = ImageChops.invert(hallo_color_layer)
        image = Image.composite(image,hallo_color_layer,hallo_mask_layer)
    
 
    draw.text(location, text, font=font, fill=255-opacity)
    # composite the watermark with the layer
    return Image.composite(image,color_layer,mask_layer)