当前位置:网站首页>Haven't tried line art videos this year??

Haven't tried line art videos this year??

2022-08-09 21:36:00 knighthood2001

  

🥰 博客首页:knighthood2001

欢迎点赞评论️

️ 热爱python,期待与大家一同进步成长!!️

给大家推荐一款很火爆的刷题、面试求职网站

跟我一起来巩固基础和刷题吧

先看后赞,已成习惯(只截取了一部分,Afraid to intercept too much,You open the card)

目录

前言

1The original video is extracted frame by frame.py

2Raw video audio extraction.py

3-1PILBatch convert detail frame line art.py

3-2PILBatch convert edge-enhanced line art.py

4PIL帧-视频合成.py

5Audio and video synthesis The final full video with sound.py

总结


前言

入门opencv,Laugh and be happy every day

The general idea is similar to the previous article,这里采用了opencv+PIL+moviepy,Finally made a line art with soundMP4.

Here I talk a little aboutPIL的知识:利用PILlibrary for simple image manipulation,The kids next door who beat the king are asking me for sketches,快上车!!

目录如下:A video needs to be prepared

The code is mainly divided into the following parts:

        1The original video is extracted frame by frame

        2Raw video audio extraction

        3-1PILBatch convert detail frame line art  3-2PILBatch convert edge-enhanced line art

        4PIL帧-视频合成

        5Audio and video synthesis The final full video with sound

注:需要更改的,The author has commented in the codetodo了.


1The original video is extracted frame by frame.py

import os
import cv2
# todo
cap = cv2.VideoCapture('ikun.mp4')

fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print('fps:', fps, '\n', 'width:', width, '\n', 'height:', height, '\n', 'frames:', frames)
# todo
path = 'ikun'
if not os.path.exists(path):
    os.mkdir(path)

i = 0
while True:
    flag, frame = cap.read()
    filename = path + '/{}.jpg'.format(str(i))
    print(filename)
    cv2.imwrite(filename, frame)
    i = i + 1
    if i > int(frames):
        break

        在上篇文章中,The author directly continues the frames extracted from the original video边缘检测Save as a picture after processing,而在这里,The author directly extracts the frames of the original video and saves them without modification.便于后续操作. 


2Raw video audio extraction.py

import moviepy.editor as mp
def extract_audio(videos_file_path):
    my_clip = mp.VideoFileClip(videos_file_path)
    my_clip.audio.write_audiofile(f'{videos_file_path.split(".")[0]}.mp3')
# todo
extract_audio('ikun.mp4')

3-1PILBatch convert detail frame line art.py

import os
from PIL import Image
from PIL import ImageFilter

# todo 这里的pathFor the previously extracted picture frame by frame,new_pathDirectory to save for newly generated line drawings
path = 'ikun'
new_path = 'new_ikun1'

if not os.path.exists(new_path):
    os.mkdir(new_path)

# Output the number of original catalog images
a = os.listdir(path)
b = len(a)
print("The number of original catalog images:", b)
for i in range(b):
    '''细节'''
    square = Image.open(path + "/{}.jpg".format(i))
    square1 = square.filter(ImageFilter.DETAIL)
    '''轮廓'''
    square2 = square1.filter(ImageFilter.CONTOUR)
    square2.save(new_path + "/{}.jpg".format(i))
    print(new_path + "/{}.jpg".format(i))

这里笔者采用了PIL中细节+Outline way,Extract line drawings(PILExtract line drawings,An outline is required for this step)

Picture change display

3-2PILBatch convert edge-enhanced line art.py

import os
from PIL import Image
from PIL import ImageFilter

# todo 这里的pathFor the previously extracted picture frame by frame,new_pathDirectory to save for newly generated line drawings
path = 'ikun'
new_path = 'new_ikun2'

if not os.path.exists(new_path):
    os.mkdir(new_path)

# Output the number of original catalog images
a = os.listdir(path)
b = len(a)
print("The number of original catalog images:", b)
for i in range(b):
    '''边缘增强'''
    square = Image.open(path + "/{}.jpg".format(i))
    square1 = square.filter(ImageFilter.EDGE_ENHANCE)
    '''轮廓'''
    square2 = square1.filter(ImageFilter.CONTOUR)
    square2.save(new_path + "/{}.jpg".format(i))
    print(new_path + "/{}.jpg".format(i))

同样的,I just changed it herePIL的一个函数,Use edge enhancement+Outline way,You can check out my previous blog,You can also try it yourself,Try out a variety of styles.

 利用PILlibrary for simple image manipulation,The kids next door who beat the king are asking me for sketches,快上车!!

Picture change display:

 

(不知道为啥,I feel this looks better)


4PIL帧-视频合成.py

import cv2
import os
size = (854, 480)
# todo pathAnd the saved video name needs to be changed according to
path = 'new_ikun1'
videowrite = cv2.VideoWriter('output_ikun1.mp4', -1, 25, size)

a = os.listdir(path)
n = len(a)
for i in range(n):
    img = cv2.imread(path + "/{}.jpg".format(i))
    videowrite.write(img)
videowrite.release()
print('end!')

5Audio and video synthesis The final full video with sound.py

import moviepy.editor as mp

# todo The path depends on the situation
# todo Incoming lineart video
video = mp.VideoFileClip('output_ikun1.mp4')
audio = mp.AudioFileClip('ikun.mp3')
video_merge = video.set_audio(audio)
# todo The final generated video with audio
video_merge.write_videofile('final_ikun1.mp4')



# video = mp.VideoFileClip('output_ikun2.mp4')
# audio = mp.AudioFileClip('ikun.mp3')
# video_merge = video.set_audio(audio)
# video_merge.write_videofile('final_ikun2.mp4')

注:4和5are only based on3-1The line art content for making a video


总结

        It's almost finished,Everyone is not familiar with the content of this article,See my previous article,里面有详细讲解.

        It will be picked up next if available改为函数,It is convenient for everyone to change and use!!

        注:The above content is only for discussion术,It is convenient for everyone to be interested in it!!

原网站

版权声明
本文为[knighthood2001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091824461141.html