Python Pillow write text on the image

Writing Text on an Image with Python Pillow

You can write text on an image by passing the position of the text, the text itself, and the color of the text. You can also pass multiple other parameters to this method.

Examples

from PIL import Image, ImageDraw

img = Image.open(beach1.jpg')
d1 = ImageDraw.Draw(img)
d1.text((28, 36), "Hello, TutorialsPoint!", fill=(255, 0, 0))
img.show()
img.save("images/image_text.jpg")

Input

Python Pillow - Write text on images

Output

If you save the above program as Example.py and execute it, it will append the given text and display it using standard PNG display tools, as shown below.

Python Pillow - Writing Text on an Image

Selecting a Font

There are a number of ways to select a font for writing on an image. We can load a font directly from the system by passing the full path to the function, or we can use ImageFont to load TrueType fonts.

Examples

from PIL import Image, ImageDraw, ImageFont

img = Image.open('images/logo.jpg')
d1 = ImageDraw.Draw(img)
myFont = ImageFont.truetype('E:/PythonPillow/Fonts/FreeMono.ttf', 40)
d1.text((0, 0), "Sample text", font=myFont, fill =(255, 0, 0))
img.show()
img.save("images/image_text.jpg")

Output

Python Pillow - Write text on images

Leave a Reply

Your email address will not be published. Required fields are marked *