How to Add Text to Image in Python?


Adding text to images is a great way to make your message more visible and engaging. Python makes it easy to add text to images. In this blog post, we’ll show you how to add text to images using the Python programming language.

Python is a versatile language that can be used for a wide variety of applications. Python is popular among developers because it is easy to learn and use. Adding text to image files is one of the many things that can be done with Python.

Adding Text to Images with Python

Adding text to images with Python is easy and straightforward. Here are the steps that you need to follow:

1. Install the Pillow package. The Pillow package is used for handling images in Python. It can be installed using pip, which is the package manager for Python packages.

$pip install pillow
Code language: PHP (php)

2. Load the image file using the Pillow package.

img = Image.open("/code/image.jpeg")
Code language: JavaScript (javascript)

3. Create a new Pillow ImageDraw object. This object will be used for adding text to the image file.

draw = ImageDraw.Draw(img)


4. Specify the font type, font size, and color of the text that you want to add to the image file.

font = ImageFont.truetype("times-ro.ttf", 24)
Code language: JavaScript (javascript)

5. Use the ImageDraw object’s text() method to add text to the image file at the desired location.

txt = "Best platform" draw.text((250, 250), txt, fill =(0, 0, 0), font=font)
Code language: JavaScript (javascript)

6. Save the modified image file under a new name or overwrite the original image file.”

img.save('output/graph.png') img.show()
Code language: JavaScript (javascript)

Another full python script

# run pip install pillow on terminal from PIL import Image # importing required libraries from PIL import ImageFont from PIL import ImageDraw # here we are creating an image which is a simple rectangle of 600*400 dimension but instead we can also have an image img = Image.new('RGB', (600, 400), color='red') # if we want to add text on the image we have already we do not need to write these two lines of code img.save('pil_red.png') # we have downloaded times new roman font from internet as described in readme file font = ImageFont.truetype("times-ro.ttf", 24) img = Image.new('RGB', (600, 400), color='red') draw = ImageDraw.Draw(img) # we can choose our font accordingly and the text on the image draw.text((300, 200), "Hello world !", (0, 0, 0), font=font) img.save('pil.png') # a new image is saved ,which has text on image .
Code language: Python (python)

Conclusion:

Adding text to images with Python is easy and straightforward by following these steps: install Pillow, load an image file using Pillow, create a new Pillow ImageDraw object, specify font type, font size, and color of desired text, and use the ImageDraw object’s text() method add text at desired location & save the modified image under a new name or overwrite original image file.”

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts