python - 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 格式的视频。我应该如何进行?
解决方案
我找到了这个。
他们使用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()
推荐阅读
- java - 为了线程安全,非最终字段是否也需要在构造函数中同步?
- mysql - 如果用户在 mysql 中完成了所有课程,则计算用户数
- python - 如何将地图与定义的功能一起使用
- c# - 为什么 asp.net 用户会话结束会导致不同应用程序池中的 Quartz 任务终止?
- python-3.x - 国际化 Python 项目,使用 PyGTK3 和 Glade
- c - 未使用 gcc 和 makefile 为 #ifdef 定义值
- sql - MS Access - 子查询在 Access 中不起作用(sql 有效)
- google-apps-script - 服务超时:电子表格:为什么是现在?
- c - 从同一进程创建 N 个子进程,然后打印具有相关父进程的所有子进程的 PID
- pandas - Use pandas_udf with spark 2.2