Draw text with background color
Asked Answered
Y

2

8

I want to know how can I draw text like this check image

As you can see text is on a green image and text has pink color background

My code, this is part of my code I'm using PIL

        draw = ImageDraw.Draw(background)
        font = ImageFont.truetype("assets/font2.ttf", 40)
        font2 = ImageFont.truetype("assets/font2.ttf", 70)
        arial = ImageFont.truetype("assets/font2.ttf", 30)
        name_font = ImageFont.truetype("assets/font.ttf", 30)
        para = textwrap.wrap(title, width=32)
        j = 0
        draw.text(
            (10, 10), f"{h}", fill="red", font=name_font
        )
        draw.text(
            (600, 150),
            "NOW PLAYING",
            fill="white",
            stroke_width=2,
            stroke_fill="white",
            font=font2,
        )

Thanks in advance :-)

Yaroslavl answered 15/5, 2022 at 17:29 Comment(1)
Does this help? #2499375Tradition
K
20

You can use the draw.textbbox method to get a bounding box for your text string and fill it using the draw.rectangle method.

from PIL import Image, ImageDraw, ImageFont

image = Image.new("RGB", (500, 100), "white")
font = ImageFont.truetype("segoeui.ttf", 40)
draw = ImageDraw.Draw(image)
position = (10, 10)
text = "Hello world"

bbox = draw.textbbox(position, text, font=font)
draw.rectangle(bbox, fill="red")
draw.text(position, text, font=font, fill="black")

image.show()

Example 1, black text on red rectangle

If you want a larger margin for the background rectangle, you can adjust the returned bounding box like so:

left, top, right, bottom = draw.textbbox(position, text, font=font)
draw.rectangle((left-5, top-5, right+5, bottom+5), fill="red")
draw.text(position, text, font=font, fill="black")

Example 2, wider rectangle in background

Kavita answered 19/5, 2022 at 10:3 Comment(1)
Thanks for this. Just a quick tip, if you want to center align the textbox, pass the attribute anchor="mm". Further explanation of anchor attribute: pillow.readthedocs.io/en/stable/handbook/…Causey
S
1
from PIL import Image, ImageDraw, ImageFont

# Create an image with white background
img = Image.new('RGB', (800, 1200), color='white')
draw = ImageDraw.Draw(img)

# Load a font
font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
font_text = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)

# Define text contents
contents = {
    "title": "ملخص حول الوضع التنموي في نيجيريا",
    "sections": [
        {
            "heading": "مقومات الغنى الطبيعي في نيجيريا",
            "subsections": [
                {
                    "subheading": "على مستوى الفلاحة والصيد البحري:",
                    "points": [
                        "تقع البلاد على المحيط الأطلسي.",
                        "أهمية الفلاحة: الزراعة تنتج القطن، الكاكاو، والبن.",
                        "أهمية الصيد البحري: وجود مناطق صيد واسعة."
                    ]
                },
                {
                    "subheading": "على مستوى الطاقة والمعادن:",
                    "points": [
                        "حقول النفط تساهم بنسبة كبيرة في دخل البلاد.",
                        "إنتاج الغاز الطبيعي والمعادن في مختلف أنحاء البلاد."
                    ]
                }
            ]
        },
       
Standifer answered 6/6 at 22:48 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Leukemia

© 2022 - 2024 — McMap. All rights reserved.