TikTok character videos have been popular for some time last year.
Anyway, I can't forget the music of the Pure Land of Bliss...
This time, I will also implement a wave by myself and make a character video.
The main libraries used are cv2 and pillow libraries.
The original video is as follows, downloaded directly from Douyin, with a proper watermark.
But it does not affect this operation.
**/ 01 / Video to Picture **
It is impossible to install the cv2 library directly on Pycharm. I don't know why.
After my practice, I found that it only needs to be in the virtual environment of Pycharm.
Run the following command to successfully install the cv2 library.
pip3 install opencv-python
However, the download speed is still too slow, resulting in a timeout.
If it doesn't work, you can go to the official website and install it in the next .whl format.
The video to picture code is as follows.
import cv2
import os
# Create a new folder in the current directory
folder_path ="img_bear/"
os.makedirs(folder_path)
# Load the video
vc = cv2.VideoCapture('bear.mp4')
c =0
# Determine whether the loaded video can be opened
ret = vc.isOpened()
# Loop read video frame
while ret:
c = c +1
# Read a single picture,The value of ret is True or Flase,frame represents the image read
ret, frame = vc.read()if ret:
# Save as image
cv2.imwrite('img_bear/'+str(c)+'.jpg', frame)
# Output image name
print('img_bear/'+str(c)+'.jpg')
# In a given time(Unit ms)Wait for the user to press the key to trigger,1ms
cv2.waitKey(1)else:break
# Video release
vc.release()
Finally, 369 pictures were successfully generated.
Cute and cute ××bear, I don’t know what kind of bear...
/ 02 / Picture to character
The ordinary picture to character picture mainly uses the pillow library.
Perform grayscale processing on the picture, and then add the corresponding characters according to the grayscale value of the picture pixel.
The specific code is as follows.
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import os
# Create character picture folder
folder_path ="bear/"
os.makedirs(folder_path)for i inrange(1,1000):
filename ='img_bear/'+str(i)+'.jpg'
# Character list
ascii_char =list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~ <>i!lI;:,\"^`'. ")
# Determine if the picture exists
if os.path.exists(filename):
# Convert pictures to grayscale images,And resize
img_array = np.array(Image.open(filename).resize((70,70), Image.ANTIALIAS).convert('L'))
# Create a new picture object
img = Image.new('L',(560,560),255)
draw_object = ImageDraw.Draw(img)
# Set font
font = ImageFont.truetype('consola.ttf',10, encoding='unic')
# Add corresponding characters according to gray value
for j inrange(70):for k inrange(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 character pictures
img.save(name,'JPEG')
Finally, the character picture was successfully generated.
Same as the original picture above, there are also 369 pictures.
/ 03 / Character to Video
Next, use the cv2 library to convert the character pictures into videos.
The specific code is as follows.
import cv2
import os
# Set up the video encoder,Use the MJPG encoder here
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# Output video parameter settings,Contains video file name, encoder, frame rate, video width and height(The parameter here must be the same as the size of the character image)
videoWriter = cv2.VideoWriter('bear_character.avi', fourcc,20.0,(560,560))for i inrange(1,1000):
filename ='bear/'+str(i)+'.jpg'
# Determine if the picture exists
if os.path.exists(filename):
img = cv2.imread(filename=filename)
# In a given time(Unit ms)Wait for the user to press the key to trigger,100ms
cv2.waitKey(100)
# Write pictures into video
videoWriter.write(img)print(str(i)+'.jpg'+' done!')
# Video release
videoWriter.release()
Finally, the character video is successfully generated.
I'm too lazy to add the original BGM here, so just make do with it.
It is said that squinting is better.
Recommended Posts