Fundamentals 2 min read

Adding Sequential Numbers to Images Using Python Pillow

This tutorial demonstrates how to use Python's Pillow library to overlay sequential numbers onto local images, creating identifiable test data for image upload and ordering verification without manually remembering each picture's appearance.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Adding Sequential Numbers to Images Using Python Pillow

When testing image upload features, it can be cumbersome to remember each image's visual content; labeling images with numbers simplifies verification. This guide shows how to programmatically write numbers onto images using the Pillow library.

Dependencies: Install Pillow via pip install Pillow.

Sample code:

# -*- coding:utf-8 -*-
from PIL import Image, ImageDraw, ImageFont

books = [1]  # image identifiers
img = '本地图片地址.JPG'          # source image path
new_img = 'text.png'            # output image with numbers
compress_img = 'compress.png'  # optional compressed version

# Font configuration
font_type = '/System/Library/Fonts/STHeiti Light.ttc'
font = ImageFont.truetype(font_type, 100)
color = "#000000"

# Open image and prepare drawing context
image = Image.open(img)
draw = ImageDraw.Draw(image)
width, height = image.size

# Positioning parameters
book_x = 5
book_start_y = 190
book_line = 50

for num, book in enumerate(books):
    y = book_start_y - num * book_line
    draw.text((book_x, height - y), u'%s' % (book), color, font)

# Save the modified image
image.save(new_img, 'png')

The script opens the specified image, draws each number from the books list at calculated positions, and saves the result as a new PNG file, enabling easy visual identification during testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pillowimage-processing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.