1. Adding text to an image
To add text to an image in Pillow, you use the ImageDraw
module, which provides a text()
method to display text at specified coordinates.
Simple text addition
from PIL import ImageDraw, ImageFont
# Create a drawing object
draw = ImageDraw.Draw(image)
# Adding text to the image
text = "Sample text"
draw.text((50, 50), text, fill="white")
# Saving the image with text
image.save("text_example.jpg")
In this example, the text "Sample text"
is added at the coordinates (50, 50)
. The fill
parameter sets the text color, which can be specified as a string ("white"
, "black"
) or in RGB format ((255, 255, 255)
).
Customizing text font
By default, Pillow uses a standard font. To choose another font and set its size, use ImageFont.truetype()
. Make sure the font file is available on your system.
# Loading a font
font = ImageFont.truetype("arial.ttf", 36)
# Adding text with a custom font
draw.text((50, 100), text, font=font, fill="yellow")
image.save("text_custom_font_example.jpg")
Here, we use the font arial.ttf
with a size of 36 pixels. If the desired font is not available, you can download it and specify the path to the file.
2. Adding a watermark
A watermark is a semi-transparent text or image overlaid on a photo to protect it from copying. It can be used to add logos or author information. In Pillow, you can create a simple text watermark.
Text watermark
# Create a drawing object
draw = ImageDraw.Draw(image)
# Text and font for the watermark
watermark_text = "© 2023 My Company"
font = ImageFont.truetype("arial.ttf", 24)
# Define coordinates for positioning the watermark
width, height = image.size
text_width, text_height = draw.textsize(watermark_text, font=font)
x = width - text_width - 10
y = height - text_height - 10
# Adding the watermark in the bottom-right corner
draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
image.save("watermarked_example.jpg")
In this example, the text watermark is added to the bottom-right corner of the image. We calculate x
and y
so the watermark is slightly offset from the image border.
3. Adding graphic elements
In addition to text, Pillow allows you to draw basic graphic elements like lines, rectangles, circles, and ellipses. This is useful for creating borders, highlighting areas, and adding geometric shapes.
Drawing lines
To draw lines, you use the line()
method, which takes the start and end coordinates of the line.
# Draw a line from the top-left corner to the bottom-right
draw.line((0, 0, width, height), fill="red", width=5)
image.save("line_example.jpg")
This code draws a red line that runs diagonally from the top-left to the bottom-right corner of the image.
Drawing rectangles
Rectangles are drawn using the rectangle()
method, which takes the coordinates of the top-left and bottom-right corners.
# Draw a rectangle
draw.rectangle((50, 50, 200, 200), outline="blue", width=3)
image.save("rectangle_example.jpg")
Here we create a rectangle with a blue outline and a line width of 3 pixels. You can also use the fill
parameter to fill the rectangle with color.
Drawing circles and ellipses
The ellipse()
method lets you draw circles and ellipses. To draw a circle, specify the same size for both the width and height.
# Draw a circle
draw.ellipse((150, 150, 250, 250), outline="green", width=4)
image.save("circle_example.jpg")
In this example, a circle with its center at (200, 200)
is created. If you set different sizes for the width and height, you’ll get an ellipse.
Drawing polygons
To draw polygons, use the polygon()
method, which takes the coordinates of the vertices of the shape.
# Draw a triangle
draw.polygon([(100, 100), (150, 50), (200, 100)], outline="purple", fill="orange")
image.save("polygon_example.jpg")
This code draws a triangle with an orange fill and a purple outline. You can create any shape by providing the coordinates of its vertices.
4. Examples
Example of creating a composition with text and graphics
Let’s combine everything mentioned above to create an image with text, a watermark, and graphic elements.
from PIL import Image, ImageDraw, ImageFont
# Load the image
image = Image.open("example.jpg")
draw = ImageDraw.Draw(image)
# Adding text
font = ImageFont.truetype("arial.ttf", 36)
draw.text((50, 50), "Demo Text", font=font, fill="yellow")
# Adding a watermark in the bottom-right corner
watermark_text = "© 2023 My Company"
font_watermark = ImageFont.truetype("arial.ttf", 24)
width, height = image.size
text_width, text_height = draw.textsize(watermark_text, font=font_watermark)
x = width - text_width - 10
y = height - text_height - 10
draw.text((x, y), watermark_text, font=font_watermark, fill=(255, 255, 255, 128))
# Drawing a line, rectangle, and circle
draw.line((0, 0, width, height), fill="red", width=5)
draw.rectangle((50, 50, 200, 200), outline="blue", width=3)
draw.ellipse((150, 150, 250, 250), outline="green", width=4)
# Saving the result
image.save("final_composition_example.jpg")
This example creates an image with text, a watermark, and several graphic elements (line, rectangle, and circle). Such compositions can be used for presentations, blogs, and creating professional-looking images.
Practical applications
- Image labeling and protection: Watermarks help protect images from unauthorized use, and text allows you to add author or company information.
- Preparing images for social media: Adding text and graphics lets you prepare images for publication, providing them with additional information.
- Infographics and data visualization: Using graphic elements like lines and shapes, you can create simple graphs and charts, making Pillow useful for creating infographics.
GO TO FULL VERSION