Fundamentals 6 min read

Create Stunning Chinese Spring Couplets with Python in 70 Lines

Learn how to generate traditional Chinese spring couplets as decorative images using a concise Python script—covering required modules, character image retrieval, background handling, layout generation, and sample outputs—with clear code snippets and usage examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Create Stunning Chinese Spring Couplets with Python in 70 Lines

Spring couplets (春联) are a traditional Chinese literary form used during the New Year, featuring balanced and concise characters that convey good wishes.

The following Python script, about 70 lines long, generates decorative couplet images using publicly available glyphs for 1,550 common Chinese characters.

Required modules

import io
from PIL import Image
import numpy as np
import requests

Download character glyphs

def get_word(ch, quality):
    # Retrieve image of a single Chinese character or uppercase letter
    # ch - character, quality - resolution: H-640, M-480, L-320
    fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch': ch}).content)
    im = Image.open(fp)
    w, h = im.size
    if quality == 'M':
        w, h = int(w*0.75), int(h*0.75)
    elif quality == 'L':
        w, h = int(w*0.5), int(h*0.5)
    return im.resize((w, h))

Download background image

def get_bg(quality):
    """Get the background image for the couplet."""
    return get_word('bg', quality)

Generate couplets

def write_couplets(text, HorV='V', quality='L', out_file=None):
    """Generate a couplet image.
    text   - couplet content, words separated by spaces
    HorV   - 'H' for horizontal, 'V' for vertical layout
    quality- character resolution: H-640, M-480, L-320
    out_file - optional output filename
    """
    usize = {'H': (640, 23), 'M': (480, 18), 'L': (320, 12)}
    bg_im = get_bg(quality)
    text_list = [list(item) for item in text.split()]
    rows = len(text_list)
    cols = max([len(item) for item in text_list])
    if HorV == 'V':
        ow, oh = 40 + rows*usize[quality][0] + (rows-1)*10, 40 + cols*usize[quality][0]
    else:
        ow, oh = 40 + cols*usize[quality][0], 40 + rows*usize[quality][0] + (rows-1)*10
    out_im = Image.new('RGBA', (ow, oh), '#f0f0f0')
    for row in range(rows):
        if HorV == 'V':
            row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white')
            offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20)
        else:
            row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white')
            offset = (20, 20+(usize[quality][0]+10)*row)
        for col, ch in enumerate(text_list[row]):
            if HorV == 'V':
                pos = (0, col*usize[quality][0])
            else:
                pos = (col*usize[quality][0], 0)
            ch_im = get_word(ch, quality)
            row_im.paste(bg_im, pos)
            row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im)
        out_im.paste(row_im, offset)
    if out_file:
        out_im.convert('RGB').save(out_file)
    out_im.show()

Example usage

# Example 1
text = '普天同庆 欢度春节'
write_couplets(text, HorV='V', quality='M', out_file='普天同庆.jpg')

# Example 2
text = '年尽岁除岁月如歌 冬去春来春光似画'
write_couplets(text, HorV='H', quality='M', out_file='岁月如歌.jpg')

# Example 3
text = '人世间纵有百娇千红 唯独你是我情之所钟'
write_couplets(text, HorV='H', quality='M', out_file='情之所钟.jpg')

Running the script produces image files that display the specified couplets on a decorative background, suitable for New Year greetings.

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.

PythonPILChinese Calligraphy
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.