Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Selected Reading

    Python Pillow - ImageDraw.multiline_text() Function



    The ImageDraw.multiline_text() method in the Pillow library is used to draw multiline text at a specified position on an image.

    Syntax

    Following is the syntax of the function −

    ImageDraw.multiline_text(xy, text, fill=None, font=None, anchor=None, spacing=4, align='left', direction=None, features=None, language=None, stroke_width=0, stroke_fill=None, embedded_color=False, font_size=None)
    

    Parameters

    Here are the details of this function parameters −

    • xy − The anchor coordinates of the text. It specifies the position where the anchor aligns with the text.

    • text − The string to be drawn, containing the multiline text.

    • fill − The color to use for the text.

    • font − An ImageFont instance that represents the font to be used for the text.

    • anchor − The text anchor alignment. It determines the relative location of the anchor to the text. The default alignment is top left.

    • spacing − The number of pixels between lines.

    • align − The alignment of lines within the multiline text. It can be "left", "center", or "right".

    • direction − The direction of the text. It can be "rtl" (right to left), "ltr" (left to right), or "ttb" (top to bottom).

    • features − A list of OpenType font features to be used during text layout. It's used to enable or disable optional font features.

    • language − The language of the text. It should be a BCP 47 language code.

    • stroke_width − The width of the text stroke.

    • stroke_fill − The color to use for the text stroke. If not given, it defaults to the fill parameter.

    • embedded_color − Whether to use font embedded color glyphs (COLR, CBDT, SBIX).

    • font_size − If the font parameter is not provided, this specifies the size to use for the default font.

    Examples

    Example 1

    In this example, the multiline_text() method is used to draw multiline text with the default parameters on the image.

    from PIL import Image, ImageDraw, ImageFont
    
    # Create an image
    image = Image.new("RGB", (700, 300), "white")
    
    # Get a drawing context
    draw = ImageDraw.Draw(image)
    
    # Define the multiline text
    text = "Hello,\nThis is\nMultiline Text!"
    
    # Set the fill color for the text
    fill_color = 'green'
    
    # Draw multiline text on the image with default parameters
    draw.multiline_text((150, 130), text, fill=fill_color)
    
    # Display the image
    image.show()
    print('The multiline text is drawn successfully...')
    

    Output

    The multiline text is drawn successfully...
    

    Output Image

    multiline text

    Example 2

    In this example, the multiline text is drawn on an image with different parameters.

    from PIL import Image, ImageDraw, ImageFont
    
    # Create a new image with a white background
    image = Image.new("RGB", (700, 300), "black")
    draw = ImageDraw.Draw(image)
    
    # Define the multiline text
    text = "Hello,\nThis is\nMultiline Text!"
    
    # Set the font and size
    font = ImageFont.truetype("arial.ttf", size=20)
    
    # Set fill color for the text
    fill_color = "orange"
    
    # Draw multiline text on the image
    draw.multiline_text((250, 100), text, fill=fill_color, font=font, spacing=10, align="left")
    
    # Display the image
    image.show()
    print('The multiline text is drawn successfully...')
    

    Output

    The multiline text is drawn successfully...
    

    Output Image

    multiline text black background

    Example 3

    The following example demonstrates how to draw the multiline text on an existing image with different parameters.

    from PIL import Image, ImageDraw, ImageFont
    
    # Open an Image
    image = Image.open('Images/TP-W.jpg')
    
    # Create the draw object
    draw = ImageDraw.Draw(image)
    
    # Define the multiline text
    multiline_text = "Tutorialspoint,\nSimple Easy Learning At Your Fingertips"
    
    # Set the font and size
    font = ImageFont.load_default()
    
    # Set fill color for the text
    fill_color = "darkgreen"
    
    # Draw multiline text on the image
    draw.multiline_text((240, 310), multiline_text, fill=fill_color, font=font, spacing=10, align="left")
    
    # Display the image
    image.show()
    
    print('Multiline text is drawn successfully...')
    

    Output

    Multiline text is drawn successfully...
    
    draw multiline text
    python_pillow_function_reference.htm
    Advertisements