Create Unique Chinese New Year Phone Wallpapers with Python and PIL
This tutorial shows how to use Python's Pillow library to add custom text, rotate images, and combine transparent layers to generate personalized Chinese New Year phone wallpapers, complete with step‑by‑step code examples and sample results.
Introduction
Hello, I am Zhu Xiao Wu. With the upcoming New Year, you can create a festive, personalized phone wallpaper using Python.
Choosing a Template
First, select an existing wallpaper template. Below are several example templates:
Using Pillow (PIL)
Pillow is a third‑party Python library for image processing. The core class Image provides methods such as save(filename, format), open(filename, mode), and paste(region, box, mask).
Simple Text Overlay Example
from PIL import Image, ImageDraw, ImageFont
a = '小王小王'
font = ImageFont.truetype('simhei.ttf', 100) # black font
img = Image.open('底图.png')
draw = ImageDraw.Draw(img)
draw.text((200, 700), a, (0, 0, 0), font=font)
img.show()
img.save('壁纸.png')The above code adds black text to the template, but the result looks plain.
Rotating the Image
Since rotating text directly is difficult, we rotate the whole image:
from PIL import Image, ImageDraw, ImageFont
a = '小王小王'
font = ImageFont.truetype('simhei.ttf', 100)
img = Image.open('底图.png')
img = img.rotate(-15) # rotate image
draw = ImageDraw.Draw(img)
draw.text((200, 700), a, (0, 0, 0), font=font)
img.show()
img.save('壁纸.png')This produces a tilted background, but not the desired slanted text effect.
Creating a Transparent Text Layer
The solution is to draw the text on a transparent image, rotate that image, and paste it onto the colored background.
from PIL import Image, ImageDraw, ImageFont
a = '小王小王'
b = '可爱大王'
font = ImageFont.truetype('hylx.ttf', 180)
# Transparent layer
img1 = Image.open('透明.png').convert('RGBA')
draw = ImageDraw.Draw(img1)
draw.text((250, 550), a, (255, 243, 211), font=font)
draw.text((350, 800), b, (255, 243, 211), font=font)
img1 = img1.rotate(15)
# Background layer
img2 = Image.open('底图.png').convert('RGBA')
img2.paste(img1, (0, 0), img1)
img2.show()
img2.save('壁纸.png')The final wallpaper shows slanted, colorful text on the festive background:
Conclusion
You can experiment with different fonts, colors, and templates to create a personalized New Year wallpaper. The same technique applies to any custom image generation task using Python and Pillow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
