如何用python中的图像制作电影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44947505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:39:21  来源:igfitidea点击:

How to make a movie out of images in python

pythonimagevideoscreenshot

提问by Victor

I currently try to make a movie out of images, but i could not find anything helpful .

我目前尝试用图像制作电影,但我找不到任何有用的东西。

Here is my code so far:

到目前为止,这是我的代码:

import time

from PIL import  ImageGrab

x =0

while True:
    try:
        x+= 1
        ImageGrab().grab().save('img{}.png'.format(str(x))
    except:
        movie = #Idontknow
        for _ in range(x):
            movie.save("img{}.png".format(str(_)))

movie.save()

回答by BoboDarph

You could consider using an external tool like ffmpeg to merge the images into a movie (see answer here) or you could try to use OpenCv to combine the images into a movie like the example here.

您可以考虑使用 ffmpeg 等外部工具将图像合并为电影(请参阅此处的答案),或者您可以尝试使用 OpenCv 将图像合并为电影,例如此处的示例。

I'm attaching below a code snipped I used to combine all png files from a folder called "images" into a video.

我在下面附上了一段代码,我用来将名为“图像”的文件夹中的所有 png 文件合并到视频中。

import cv2
import os

image_folder = 'images'
video_name = 'video.avi'

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, 1, (width,height))

for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

回答by Victor

Thanks , but i found an alternative solution using ffmpeg:

谢谢,但我找到了使用 ffmpeg 的替代解决方案:

def save():
    os.system("ffmpeg -r 1 -i img%01d.png -vcodec mpeg4 -y movie.mp4")

But thank you for your help :)

但是谢谢你的帮助:)