Python-将文件夹中的.png图像转换为视频

问题描述

我想用python建立一个屏幕录像机。因此,我使用了pyautogui库的Screenshot()函数。当我运行程序时,我在这样的文件夹中获得了单独的PNG图像-----

C:\rec\1.png
C:\rec\2.png
C:\rec\3.png
C:\rec\4.png
C:\rec\5.png

以此类推。现在,我想将这些PNG图像转换为MP4或AVI格式的视频。我应该如何进行?

解决方法

我找到了this

他们使用opencv库:

pip install opencv-python

然后:

import cv2
import numpy as np
import glob
 
img_array = []
for filename in glob.glob('C:/rec/*.png'): # (The only change I have made is here to the filepath.)
    img = cv2.imread(filename)
    height,width,layers = img.shape
    size = (width,height)
    img_array.append(img)
 
 
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'),15,size)
 
for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...