Text/Farnastt diagram/from network

\

Douyin character videos were popular for a while last year.

\

Anyway, I can’t forget the music of the pure land.

\

This time to achieve their own wave, do a character video out.

\

The main libraries used are CV2, Pillow library.

\

The original video is as follows, directly tiktok download, no problem with the watermark.

\

\

However, this operation will not be affected.

\

\

/ 01 / Video to picture

\

Installing the CV2 library directly on Pycharm was not successful, for what reason I don’t know.

\

After my practice, I found that only Pycharm’s virtual environment was needed.

\

To successfully install the cv2 library, run the following command.

\

Copy the code

pip3 install opencv-python\

\

However, the download speed is still too slow, resulting in a timeout.

\

If not, you can go to the official website, next.WHL format to install.

\

The code of video to picture is as follows.

\

Copy the code

Import cv2 import OS # Create folder folder_path = “img_bear/” os.makedirs(folder_path) # to load video vc = Cv2.videocapture (‘bear. Mp4 ‘) c = 0 # Check whether the loaded video can be opened ret = vc.isopened () # loop read video frames while ret: Frame = vc.read() if ret = True or Flase: Cv2. imwrite(‘img_bear/’+ STR (c) + ‘.jpg’, Print (‘img_bear/’+ STR (c) + ‘.jpg’) print(‘img_bear/’+ STR (c) + ‘.jpg’) print(‘img_bear/’+ STR (c) + ‘.jpg’) Vc.release ()\

\

Finally, 369 images were successfully generated.

\

\

Cute cute bear, I don’t know what kind of bear…

\

\

/ 02 / Picture to character

\

The pillow library is mainly used for converting ordinary image to character image.

\

Do gray processing to the picture, and then according to the gray value of the picture pixel, add the corresponding character.

\

The specific code is as follows.

\

Copy the code

from PIL import Image, ImageDraw, Folder_path = “bear/” os.makedirs(folder_path) for I in range(1, 1000). Filename = ‘img_bear/’ + STR (I) + ‘.jpg’ # character list ascii_char = list(“$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~ <>i! lI; If os.path.exists(filename): Img_array = np.array(image.open (filename).resize((70, 70), Img = image.new (‘L’, (560, 560), 255) draw_object = imagedraw.draw (img) # set font = imagefont. truetype(‘consola.ttf’, 10, Encoding =’unic’) # encoding=’unic’ for j in range(70): for k in range(70): x, y = k * 8, j * 8 index = int(img_array[j][k] / 4) draw_object.text((x, y), ascii_char[index], font=font, Fill =0) name = ‘bear/’ + STR (I) + ‘.jpg’ print(name) # save img.save(name, ‘JPEG’)\

\

Finally, the character picture is successfully generated.

\

Same as the original image above, 369.

\

\

/ 03 / Character to video

\

Next, use the CV2 library to convert the character picture into a video.

\

The specific code is as follows.

\

Copy the code

Fourcc = cv2.VideoWriter_fourcc(*’MJPG’) # VideoWriter = cv2. videoWriter (‘bear_character.avi’, fourcc, 20.0, (560, 560)) for I in range(1, 1000): filename = ‘bear/’+ STR (I)+’.jpg’ Img = cv2.imread(filename=filename) # Wait for the user button to trigger for a given amount of time (in ms),100ms cv2.waitKey(100) # Write the image to the video videoWriter.write(img) print(str(i) + ‘.jpg’ + ‘ done! ‘) # release videoWriter.release()\

\

Finally, the character video is successfully generated.

\

I’m too lazy to add the original BGM here, so I’ll make do.

\

\

It is said that squinting is more effective.

\

The video and the code have been uploaded,

\

Public account menu bar bottom reply ** “character” ** can be obtained.

\

Use NetworkX to map the structure of the deep neural network. Use Python to describe cookies and sessions in the library for reading and writing MySQL List of Python technical knowledge (data science) \

Click below to read the original article

Free to become a community registered member, members can enjoy more rights and interests