【Python使用OpenCV使用多个图像创建视频】众所周知, OpenCV是一个广泛使用的图像处理库。它提供了广泛的图像处理意识。让我们看看如何使用OpenCV使用多张图片创建视频。
安装以下库:PIL cv2另外, 在运行代码之前检查路径, 否则你将充满错误。
这个怎么运作 ?
使用PIL库,我们正在打开图像并调整它们的平均高度和平均宽度,因为将使用cv2库创建的视频需要相同的高度和宽度的输入图像。
调整大小的图像包含在一个数组中,视频的帧设置为均值高度和均值宽度。然后通过循环,我们将每个图像附加到那个帧。
下面是实现:
# importing libraries
import os
import cv2
from PIL import Image # Checking the current directory path
print (os.getcwd()) # Folder which contains all the images
# from which video is to be generated
os.chdir( "C:\\Python\\Geekfolder2" )
path = "C:\\Python\\Geekfolder2"mean_height = 0
mean_width = 0num_of_images = len (os.listdir( '.' ))
# print(num_of_images)for file in os.listdir( '.' ):
im = Image. open (os.path.join(path, file ))
width, height = im.size
mean_width + = width
mean_height + = height
# im.show()# uncomment this for displaying the image# Finding the mean height and width of all images.
# This is required because the video frame needs
# to be set with same width and height. Otherwise
# images not equal to that width height will not get
# embedded into the video
mean_width = int (mean_width /num_of_images)
mean_height = int (mean_height /num_of_images)# print(mean_height)
# print(mean_width)# Resizing of the images to give
# them same width and height
for file in os.listdir( '.' ):
if file .endswith( ".jpg" ) or file .endswith( ".jpeg" ) or file .endswith( "png" ):
# opening image using PIL Image
im = Image. open (os.path.join(path, file )) # im.size includes the height and width of image
width, height = im.size
print (width, height)# resizing
imResize = im.resize((mean_width, mean_height), Image.ANTIALIAS)
imResize.save( file , 'JPEG' , quality = 95 ) # setting quality
# printing each resized image name
print (im.filename.split( '\\' )[ - 1 ], " is resized" ) # Video Generating function
def generate_video():
image_folder = '.' # make sure to use your folder
video_name = 'mygeneratedvideo.avi'
os.chdir( "C:\\Python\\Geekfolder2" )images = [img for img in os.listdir(image_folder)
if img.endswith( ".jpg" ) or
img.endswith( ".jpeg" ) or
img.endswith( "png" )]# Array images should only consider
# the image files ignoring others if any
print (images) frame = cv2.imread(os.path.join(image_folder, images[ 0 ]))# setting the frame width, height width
# the width, height of first image
height, width, layers = frame.shapevideo = cv2.VideoWriter(video_name, 0 , 1 , (width, height)) # Appending the images to the video one by one
for image in images:
video.write(cv2.imread(os.path.join(image_folder, image))) # Deallocating memories taken for window creation
cv2.destroyAllWindows()
video.release()# releasing the video generated# Calling the generate_video function
generate_video()
输出如下:
文章图片
获取指向所使用的输入图像和输出视频的链接这里(https://drive.google.com/drive/folders/14Z3iASRYhob9cDohpVU-pcN9LgZ2Imqp?usp=sharing).
更好的选择:
如果我们想要更多的效果,声音和视频,那么最好使用ffmpeg python库,它提供了这种功能。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python使用OpenCV对彩色图像进行去噪
- 使用OpenCV进行图像翻译|Python
- 使用Python中的形态学操作进行图像分割
- 使用OpenCV实现图像金字塔示例|Python
- 冬奥快结束了还没有抢到冰墩墩(程序员一招让你不用排队不用愁!)
- PHP 基于 SW-X 框架,搭建高性能API架构
- 北亚数据恢复NTFS文件系统误操作导致raid5阵列中的分区被格式化的逆向操作服务器数据恢复方法
- Netty 简介《Netty In Action》 #yyds干货盘点#
- 肝魂一晚上总结(全网最全最细手把手教你PyQt5安装与使用??《??记得收藏??》)