Fundamentals 2 min read

Generating Numbered Images with Python Pillow (PIL)

This tutorial demonstrates how to programmatically add sequential numbers to a base image, generate a series of new images, and save each one using Python's Pillow library, providing a practical example of automated image processing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Generating Numbered Images with Python Pillow (PIL)

Continuing from the previous article, this tutorial shows how to add sequential numbers to a single base image, generate new images for each number, and save them using Python's Pillow library.

Below is the complete Python script. It creates a list of numbers 1‑20, loads the original image, draws each number in red with a specified font, and saves each result as a separate JPEG file.

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

'''
生成测试图片
'''
def set_tupian():
    numbers = []
    for x in range(1, 21):
        numbers.append(str(x))
    # 设置字体样式
    font_type = '/System/Library/Fonts/STHeiti Light.ttc'
    font = ImageFont.truetype(font_type, 100)
    # 打开图片
    for i in numbers:
        imageFile = "../原始图片.JPG"
        im1 = Image.open(imageFile)
        draw = ImageDraw.Draw(im1)
        #设置文字位置/内容/颜色/字体
        draw.text((160, 0), i, (255, 0, 0), font=font)
        # 另存图片
        im1.save("../" + i + ".jpg")
set_tupian()

The script demonstrates a simple image‑processing workflow that can be adapted for batch labeling, watermarking, or creating numbered datasets.

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.

PythonautomationImage Generationcoding tutorial
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.