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.
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.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.