0
0
PythonHow-ToBeginner · 3 min read

How to Add Text to Image in Python Easily

You can add text to an image in Python using the Pillow library by opening the image, creating a drawing context with ImageDraw.Draw, and then using the text() method to place text at a desired position.
📐

Syntax

To add text to an image, you use the Pillow library's ImageDraw.Draw object. The main method is text(position, text, fill, font), where:

  • position: Tuple (x, y) for text location.
  • text: The string you want to add.
  • fill: Color of the text (e.g., 'black' or (255,255,255)).
  • font: Optional font style and size.
python
from PIL import Image, ImageDraw, ImageFont

image = Image.open('image.jpg')
draw = ImageDraw.Draw(image)
position = (50, 50)
text = 'Hello, World!'
fill = 'black'
# Optional: font = ImageFont.truetype('arial.ttf', 36)
draw.text(position, text, fill=fill)  # Add text to image
image.save('output.jpg')
💻

Example

This example opens a blank white image, adds black text at position (10, 10), and saves the result as output.png. It shows how to use Pillow to add text simply.

python
from PIL import Image, ImageDraw, ImageFont

# Create a blank white image
image = Image.new('RGB', (200, 100), color='white')

draw = ImageDraw.Draw(image)
text = 'Hello, Python!'
position = (10, 10)
fill = 'black'

# Optional: load a font
# font = ImageFont.truetype('arial.ttf', 24)

# Add text to image
# draw.text(position, text, fill=fill, font=font)  # with font

draw.text(position, text, fill=fill)  # without font

# Save the image
image.save('output.png')
Output
Creates a file named 'output.png' with the text 'Hello, Python!' in black on a white background.
⚠️

Common Pitfalls

  • Not installing Pillow library: Use pip install pillow before running code.
  • Forgetting to save the image after drawing text.
  • Using a font file that does not exist or is not accessible.
  • Incorrect position coordinates causing text to be outside the image.
  • Not specifying fill color, which defaults to black but can be confusing.
python
from PIL import Image, ImageDraw

image = Image.new('RGB', (100, 50), 'white')
draw = ImageDraw.Draw(image)

# Wrong: forgetting to save
# draw.text((10, 10), 'Hi')

# Right: save after drawing

draw.text((10, 10), 'Hi', fill='black')
image.save('correct_output.png')
Output
Creates 'correct_output.png' with 'Hi' text visible.
📊

Quick Reference

Here is a quick summary of key functions and parameters:

Function/ParameterDescription
Image.open(path)Open an existing image file.
Image.new(mode, size, color)Create a new blank image.
ImageDraw.Draw(image)Create a drawing context for the image.
draw.text(position, text, fill, font)Add text at position with color and optional font.
image.save(path)Save the modified image to a file.

Key Takeaways

Use Pillow's ImageDraw.Draw and its text() method to add text to images.
Always save the image after adding text to keep changes.
Specify text position and color clearly to control appearance.
Install Pillow with pip before running image text code.
Use ImageFont to customize font style and size if needed.