Creating a Cherry Blossom Timelapse with Python: Image Processing and Video Generation
This article demonstrates how to use Python, OpenCV, and Pillow to programmatically generate frames that depict the gradual opening of cherry blossoms, assemble them into a video, and share the result as a timelapse celebrating Wuhan University's spring scenery.
The post introduces a special project launched by Wuhan University to livestream cherry blossom scenes and explains how a student used Python to simulate the blossom opening process, which later became a popular online showcase.
First, each video frame is saved as a JPEG image using OpenCV's cv2.imwrite("pic/frame%d.jpg" % count, image) # save frame as JPEG file .
Next, a blank canvas is created with Pillow, a font is loaded, and a nested loop iterates over the image pixels. For each sampled pixel, the corresponding color is used to set the drawing ink and render the Chinese phrase "武汉加油" on the canvas:
<code>blank = Image.new("RGB", [len(img[0]), len(img)], "white")
drawObj = ImageDraw.Draw(blank)
n = 10
font = ImageFont.truetype("C:/Windows/Fonts/Microsoft YaHei UI/msyhbd.ttc", size=n-1)
for i in range(0, len(img), n):
for j in range(0, len(img[i]), n):
text = "武汉加油"
drawObj.ink = img[i][j][0] + img[i][j][1]*256 + img[i][j][2]*256*256
drawObj.text([j, i], text[int(j/n) % len(text)], font=font)
print("完成处理——", i, j)
blank.save("new/new_" + pic, "jpeg")
</code>Finally, the generated images are compiled into a video file. The picvideo function reads all JPEG frames, sets the video codec, frame rate, and resolution, and writes each frame to an MP4 file:
<code>def picvideo(path, size):
filelist = os.listdir(path)
filelist = resort(filelist)
fps = 24
file_path = "video/new.mp4"
fourcc = cv2.VideoWriter_fourcc('D','I','V','X')
video = cv2.VideoWriter(file_path, fourcc, fps, size)
for item in filelist:
if item.endswith('.jpg'):
img = cv2.imread(os.path.join(path, item))
video.write(img)
video.release()
</code>The resulting video shows thousands of "武汉加油" characters forming a blooming cherry blossom, symbolizing support for Wuhan and demonstrating practical image‑processing techniques with Python.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.