【问题标题】:turn PIL images into video on Linux在 Linux 上将 PIL 图像转换为视频
【发布时间】:2018-09-19 20:59:34
【问题描述】:

我在 Debian Linux 上使用 python 3,我正在使用 python 成像库(枕头叉)来生成一系列图像,我想以广泛支持的格式将图像输出为视频(不关心太多了,只要是我可以在 VLC 中查看并导入视频编辑软件的东西。

我该怎么做?

【问题讨论】:

    标签: python linux python-3.x video debian


    【解决方案1】:

    一个解决方案是使用opencv,数据可以通过numpy从PIL桥接到opencv。我使用的代码大纲是。

    import numpy as np
    from PIL import Image, ImageDraw
    import cv2
    
    videodims = (100,100)
    fourcc = cv2.VideoWriter_fourcc(*'avc1')    
    video = cv2.VideoWriter("test.mp4",fourcc, 60,videodims)
    img = Image.new('RGB', videodims, color = 'darkred')
    #draw stuff that goes on every frame here
    for i in range(0,60*60):
        imtemp = img.copy()
        # draw frame specific stuff here.
        video.write(cv2.cvtColor(np.array(imtemp), cv2.COLOR_RGB2BGR))
    video.release()
    

    注意事项:

    • 您需要安装相关软件包,如果使用 pip,它们是“opencv-python”、“pillow”和“numpy”。如果使用 debian 包,这些包是“python3-opencv”、“python3-pil”和“python3-numpy”。
    • 我从https://blog.extramaster.net/2015/07/python-pil-to-mp4.html得到了基本技术)
    • Extramaster 声称传递 -1 作为编解码器参数会弹出编解码器选择列表,这对我不起作用。
    • 由于早期版本缺少 python3-opencv 软件包,我最终不得不使用 Debian buster(=在撰写本文时正在测试)。
    • 如果您的帧尺寸与视频尺寸不匹配,opencv 似乎会默默地无法添加帧。

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2021-02-06
      • 2016-05-29
      • 2017-02-26
      • 2018-07-16
      相关资源
      最近更新 更多