Creating a Cherry Blossom Animation with Python, OpenCV, and Pillow
This article demonstrates how to use Python, OpenCV, and Pillow to capture, annotate, and assemble cherry‑blossom images into a video, explaining pixel color representation, frame saving, canvas creation, text rendering, and video encoding steps with complete code examples.
Wuhan University launched a live‑stream of cherry blossoms and a student used Python to simulate the blooming process, releasing the code for public use.
The tutorial explains that each image consists of pixels whose colors are represented by three integer values (0‑255) for the red, green, and blue channels. When enough pixels are present, the image appears high‑definition; too few pixels cause visible jagged edges.
To generate the animation, the following steps are performed:
1. Save each frame as a JPEG file using OpenCV:
cv2.imwrite("pic/frame%d.jpg" % count, image) # save frame as JPEG file2. Create a blank canvas with Pillow, choose a font and size, and prepare a drawing object:
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)3. Loop over the image pixels, write the text "武汉加油" with the corresponding pixel color, and save the canvas:
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")4. Combine the generated images into a video file:
def picvideo(path, size): # path = r"C:/Users/Administrator/Desktop/huaixiao" # directory containing images filelist = os.listdir(path) # list all files filelist = resort(filelist) fps = 24 file_path = "video/new.mp4" # output video 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_path = os.path.join(path, item) img = cv2.imread(img_path) # BGR image, values 0‑255 video.write(img) # write frame video.release() # finalizeThe article concludes with a disclaimer that the content is collected from the internet and credits the original author.
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 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.
