SoFunction
Updated on 2024-10-30

Python implementation of video conversion to character drawing details

Last time I wrote aHuaqiang buy melon character videopost, someone below asked how to save it, so this time I'll write a post that will save the character drawing video, however, time is not on my side, the Huaqiang Era is over, and now it's the era of the piercer.

First read the video and convert it to characters. The video was downloaded from B site at"Battle of Gangnam" Pangolin Name Scene

As the video downloaded directly from the B station is in flv format, which is not supported by imageio, although it can be read with opencv, it is not complicated to convert a code with ffmepg in comparison, so that you can maximize the use of the code of Huaqiang buy melon.

Also, the video footage is too long for a code demo, so a 15s cut is made from 2:10.

At the command line, type

>pip install ffmpeg
>ffmpeg  -i  -ss 00:02:10 -t 15 seg.mp4 -y

Soon got seg.mp4, the next step is to read the video and convert it to characters, about the parsing of this part of the code, you can go out the door and turn leftbuy melon from Huaqiang

import imageio
import numpy as np
import  as plt
video = imageio.get_reader('seg.mp4')
imgs = [(im,2) for im in video]
(imgs[30])
()

This video is too big for character art, so it needs to be compressed before converting it to character art. Here the simplest method is taken - i.e. averaging the neighboring pixels.

# Reduce the width of the image to width
from itertools import product   # for loop nesting
def resizeImg(img,w,h=None):
    m,n = 
    if n<w:
        return img
    if not h:
        h = int(m*w/n)
    im = ([h,w])
    rw,rh = n/w,m/h         # Scaling
    dw,dh = int(rw),int(rh) # Steps to take the mean
    for i,j in product(range(h),range(w)):
        I,J = int(i*rh),int(j*rw)
        im[i,j] = (img[I:I+dh,J:J+dw])
    return im
# Test it

im = resizeImg(imgs[30],160)
(im)
()

The next generation of character drawings, here using matplotlib text to draw, for the sake of viewing considerations, cancel the axes. Considering the need for consistent width of the character drawing, local fonts are enabled.

For drawing fonts, seeCalling local fonts when drawing in python

pixels = "B8&WMZO0QJX@%&jfoavunxr#t/\|()1{}[]? -_+~<>i!lI;:,\"^`'. ^`'. " # Characters used for mapping

def im2txt(img):
    im = (img/255*len(pixels)).astype(int)
    txts = ""
    for line in im:
        txts += "".join([pixels[i] for i in line])
        txts += '\r\n'    #Pixel line breaks with text also line breaks
    return txts

#Testing
txt = im2txt(im)

(figsize=(8,4.5))
['-serif'] = 'SIMSUN'  #SIMSUN in SONG
([0,10,0,10])
_ = (5, 5, txt, fontsize=6, linespacing=0.6,ha='center', va='center',wrap=True)
().set_axis_off()
()

The results are as follows

The next step is to move, and for old fans this one is obviously easy, and belongs to the traditional art of this series of PythonArt.

from matplotlib import animation
fig = (figsize=(8,4.5))
['-serif'] = 'SIMSUN'
ax = fig.add_subplot(xlim=(0,10),ylim=(0,10))
ax.set_axis_off()

text = (5, 5, txt, fontsize=6, linespacing=0.6,  ha='center', va='center',wrap=True)

def animate(im):
    text.set_text(im2txt(im))
    return [text]

imgs = [resizeImg(im,160) for im in imgs]

ani = (fig, animate, imgs[:200], interval=10, blit=True)

()

end up

to this article on the Python video conversion to character painting details of the article is introduced to this, more related Python video to character painting content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!