current position:Home>Python tiktok character video, CV2 module, Python implementation

Python tiktok character video, CV2 module, Python implementation

2022-02-01 18:45:22 Dai mubai

Time is not negative , Create constantly , This article is participating in 2021 Year end summary essay contest

Preface

utilize Python Tiktok character video . I don't say much nonsense .

Let's start happily ~

development tool

Python edition : 3.6.4

Related modules :

cv2 modular ;

PIL modular ;

numpy modular ;

As well as some Python Built in modules .

Environment building

install Python And add to environment variable ,pip Install the relevant modules required .

The main libraries used are cv2,pillow library .

The original video is as follows , Tiktok downloaded .

Video to picture

Run the following command , Can be installed successfully cv2 This library .

pip3 install opencv-python
 Copy code 

Or next .whl Format to install

The code implementation 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 frames 
while ret:
    c = c + 1
    #  Read a single picture ,ret The value of is True perhaps Flase,frame Represents the picture read in 
    ret, frame = vc.read()
    if ret:
        #  Store as image 
        cv2.imwrite('img_bear/'+str(c) + '.jpg', frame)
        #  Output image name 
        print('img_bear/'+str(c) + '.jpg')
        #  In a given time ( Company ms) Wait for the user key to trigger ,1ms
        cv2.waitKey(1)
    else:
        break
#  Video release 
vc.release()
 Copy code 

Generated 369 A picture

1.jpg

Picture to character

Ordinary pictures to character pictures are mainly used pillow library .

Grayscale the picture , Then according to the gray value of picture pixels , Add corresponding characters .

Code implementation

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 in range(1, 1000):
    filename = 'img_bear/' + str(i) + '.jpg'
    #  A list of characters 
    ascii_char = list("[email protected]%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~ <>i!lI;:,\"^`'. ")
    #  Judge whether the picture exists 
    if os.path.exists(filename):
        #  Turn the picture into a grayscale image , And resize 
        img_array = np.array(Image.open(filename).resize((70, 70), Image.ANTIALIAS).convert('L'))
        #  Create a new image object 
        img = Image.new('L', (560, 560), 255)
        draw_object = ImageDraw.Draw(img)
        #  Set the font 
        font = ImageFont.truetype('consola.ttf', 10, encoding='unic')
        #  Add the corresponding characters according to the gray value 
        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 character picture 
        img.save(name, 'JPEG')
 Copy code 

Generated 369 Character picture

2.jpg

Character to video

Use cv2 library , Convert character pictures to video .

Code implementation

import cv2
import os

#  Set up video encoder , Use... Here MJPG Encoder 
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
#  Output video parameter setting , Include video file name 、 Encoder 、 Frame rate 、 Video width and height ( The parameters here must be consistent with the size of the character picture )
videoWriter = cv2.VideoWriter('bear_character.avi', fourcc, 20.0, (560, 560))

for i in range(1, 1000):
    filename = 'bear/'+str(i)+'.jpg'
    #  Judge whether the picture exists 
    if os.path.exists(filename):
        img = cv2.imread(filename=filename)
        #  In a given time ( Company ms) Wait for the user key to trigger ,100ms
        cv2.waitKey(100)
        #  Write pictures to video 
        videoWriter.write(img)
        print(str(i) + '.jpg' + ' done!')
#  Video release 
videoWriter.release()
 Copy code 

Successfully generated character video

gif Tiktok character .gif

copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011845210569.html

Random recommended