当前位置:网站首页>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
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.
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!!
边栏推荐
猜你喜欢
Open Source Summer | List Details Display Based on Ruoyi Architecture
ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
Iptables防火墙常见的典型应用场景
正则表达式(全)
工大科雅深交所上市:市值45亿 齐承英家族是大股东
2022深圳(软考中级)系统集成项目管理工程师报名
pytest框架之mark标记功能详细介绍
数学建模——模拟退火
IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN
《痞子衡嵌入式半月刊》 第 60 期
随机推荐
为什么maxcompute的数据导入到mysql会乱码?mysql的表是udf8mb4的编码
没有 accept,建立 TCP 连接,可以吗?
[免费专栏] Android安全之Android奇淫run-as命令
典型的数据仓库模型实施过程详解
[免费专栏] Android安全之和平精英(FZ)APK逆向分析
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
三星旗舰优惠千八,苹果优惠过千,国产旗舰只降五百打发叫花子
How to stop the test after reaching a given number of errors during stress testing in JMeter
程序健壮性
AWK使用
numpy中nan_to_num如何使用
An overview of Office 365 Groups and how to create them
OpenHarmony如何查询设备类型
pytest框架之mark标记功能详细介绍
电商项目架构图
IDEA tools commonly used configuration
2022深圳(软考高级)信息系统项目管理师认证报名
MYSQL物理存储文件的页和INNOBUF的页是否有大小区别?
Iptables防火墙常见的典型应用场景
你应该试着独自做个游戏