Wu university students use Python to knock out Cherry Blossom open (with source code)

Posted by vidhatanand on Wed, 25 Mar 2020 10:32:13 +0100

Yunqi information:[ Click to see more industry information]
Here you can find the first-hand cloud information of different industries. What are you waiting for? Come on!

This year's Wuhan University is not as crowded as before, but the beautiful cherry blossom is still blooming alone in spring

Wuda has opened a live broadcast of Cherry Blossom cloud, from March 16 to March 25, from 10:00 to 16:00 every day. Meet Wu daguanbo and see cherry blossoms in different places every day.

At the same time, Zhu Yongchun, a student of Wuhan University's Information Management Institute, used Python to knock out cherry blossoms and open them, triggering a hot search.

Later, Wuda Weibo also opened source code:

In this way, we can realize the whole process of Cherry Blossom opening on our own computer A kind of



Follow the steps in the figure.

Let's talk about the principle here (quoted from Python programming time).

In fact, every picture is made up of pixels. Each pixel has its own color, which can be represented by an array: (a,b,c), where the value range of each digit is 0-255.

For example, (0,0,0) for black, (255255255) for white.

When there are enough pixels, this picture is what we call high-definition picture.

And if there are too few pixels, our naked eye can perceive the obvious sense of sawtooth.

First, use the cv2.imwrite() function of opencv to read and write, and then save the image with the suffix '. jpeg'

cv2.imwrite("pic/frame%d.jpg" % count, image)  # save frame as JPEG file

Create a new canvas, and then select the font and font size.

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)

Write a for loop to generate data, add corresponding color fonts to these words, and write them to the created canvas.

for i in range(0, len(img), n):
    for j in range(0, len(img[i]), n):
        text = 'Wuhan refueling'
        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('Finish processing——', i, j)

blank.save('new/new_' + pic, 'jpeg')

Finally, write the picture into the video and export it.

def picvideo(path, size):
   # path = r'C:\Users\Administrator\Desktop\1\huaixiao\\'#File path
   filelist = os.listdir(path)  # Get all file names in this directory
   filelist = resort(filelist)

   '''
   fps:
   //Frame rate: n pictures are written in one second [control one picture to stay for 5 seconds, that is, frame rate is 1, repeat this picture 5 times]
   //If there are 50 534 * 300 pictures in the folder, and there are 5 pictures to play in one second, then the duration of this video is 10 seconds
   '''
   fps = 24
   # size = (591,705) #Resolution slice of picture
   file_path = 'video/new.mp4'# export path
   fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')  # Different video codes correspond to different video formats (for example, 'I','4','2','0' correspond to avi format)

   video = cv2.VideoWriter(file_path, fourcc, fps, size)

   for item in filelist:
       if item.endswith('.jpg'):  # Determine whether the image suffix is. png
           item = path + '/' + item
           img = cv2.imread(item)  # Use opencv to read the image and directly return the numpy.ndarray object. The channel order is BGR. Note that it is BGR. The default range of channel value is 0-255.
           video.write(img)  # Write pictures into video

   video.release()  # release

Ten million lines of code, love you as the first line!

Countless "come on Wuhan" come together, each word is a pixel, knocking out a cherry blossom's delayed opening for you A kind of

Look at a blooming flower, wait for it to bloom into the sea, and cheer for Wuhan with Wuda cherry blossom!

[yunqi online class] product technology experts share every day!
Course address: https://yqh.aliyun.com/zhibo

Join the community immediately, face to face with experts, and keep abreast of the latest news of the course!
[yunqi online classroom community] https://c.tb.cn/F3.Z8gvnK

Original release time: March 24, 2020
Author: Zhu Xiaowu
This article comes from:“ Big data DT WeChat official account ”, you can pay attention to“ Big data DT "

Topics: Python OpenCV Big Data Spring