A, effects,

The original:

After the transformation:

The effect can be adjusted with parameters in the first few lines of the code

Second, the code

The first few lines of code, look at the adjustment, adjust the effect to the best OK.

Dependent libraries:

pip install opencv-python

pip install pygame

import pygame
import cv2
 
FONT_SIZE = 18  # font size, adjustable
WIN_SIZE = (1440.1000)  # Window size, adjustable
VIDEO_SIZE = (30.30)  # Video size, adjustable
VIDEO_PATH = './cat.gif'  # Video file (can be common video format and GIF)
STR_TEXT = 'Pretending to be insane'  # Replace character, customizable, no length limit, but must have at least one
 
 
def video2imgs(video_name, size) :
    img_list = []
    cap = cv2.VideoCapture(video_name)
 
    while cap.isOpened():
        ret, frame = cap.read()
        if ret:
            img = cv2.resize(frame, size, interpolation=cv2.INTER_AREA)
            img_list.append(img)
        else:
            break
    cap.release()
 
    return img_list
 
 
Initialize PyGame
def main() :
    pygame.init()
 
    winSur = pygame.display.set_mode(WIN_SIZE)
 
    imgs = video2imgs(VIDEO_PATH, VIDEO_SIZE)
 
    btnFont = pygame.font.SysFont("fangsong", FONT_SIZE)
 
    btnFont.set_bold(True)
 
    # generated surface
    sur_list = []
    for img in imgs:
        height, width, color = img.shape
        surface = pygame.Surface(WIN_SIZE)
        a = 0
        x, y = 0.0
        for row in range(height):
            x = 0
            for col in range(width):
                Get current pixel RGB
                rgb = img[row][col]
                rgb[0], rgb[2] = rgb[2], rgb[0]
                text_texture = btnFont.render(STR_TEXT[a], True, rgb)
                a = a + 1
                a = a % len(STR_TEXT)
                surface.blit(text_texture, (x, y))
                x = x + FONT_SIZE
            y = y + FONT_SIZE
        sur_list.append(surface)
 
    # Game main loop
    current_frame = 0
    while True:
 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
 
        pygame.time.delay(int(1000 / 24))
        winSur.fill((0.0.0))
        winSur.blit(sur_list[current_frame], [0.0])
        current_frame += 1
        current_frame %= len(sur_list)
        # Refresh interface
        pygame.display.flip()
 
 
if __name__ == '__main__':
    main()
Copy the code