python实现视频转gif

#!/usr/bin/ python
# -*- encoding: utf-8 -*-
'''
@文件    :1.py
@说明    :
@时间    :2022/03/14 10:09:07
@作者    :刘子沫
@邮箱    :spiritai@qq.com
@版本    :1.0
'''


import cv2
from PIL import Image


def convert_mp4_to_gif(input_file,output_file,duration=50,step=2):
    '''
    传参 :  input_file 视频文件名
            output_file gif文件名
            duration 每帧图像的停留时间 毫秒ms 
            step 跳帧,降低采样率,减小gif体积
    返回 : 无
    '''
    video_capture = cv2.VideoCapture(input_file)
    still_reading, image = video_capture.read()
    frame_count = 0
    i = 0
    frames = []
    while still_reading:
        still_reading, image = video_capture.read()
        if not still_reading:
            break
        if i>step:
            frames.append(Image.fromarray(cv2.cvtColor(image.copy(),cv2.COLOR_BGR2RGB)))
            frame_count += 1
            i=0
        i+=1
    frame_one = frames[0]
    frame_one.save(output_file, format="GIF", append_images=frames[1:],save_all=True, duration=duration, loop=0)




if __name__ == "__main__":
    convert_mp4_to_gif("fall-03-cam0.mp4", "fall-03-cam0.gif",100,3)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图像处理大大大大大牛啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值