web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

Python语法如何实现MP4与GIF格式互转(附代码)

587 ℃

如何利用Python语法实现MP4与GIF格式互转,下面web建站小编给大家简单介绍一下具体实现代码!

代码如下:

import glob
import cv2
from PIL import Image

def convert_mp4_to_jpgs(input_file):
# 先将mp4文件的所有帧读取出保存为图片
video_capture = cv2.VideoCapture(input_file)
still_reading, image = video_capture.read()
frame_count = 0
while still_reading:
cv2.imwrite(f"output/frame_{frame_count:03d}.jpg", image)
# read next image
still_reading, image = video_capture.read()
frame_count += 1

def convert_images_to_gif(output_file):
# 读取目录下图片,用Pillow模块的Image和所有图片合并
# 成一张gif
images = glob.glob(f"output/*.jpg")
images.sort()
frames = [Image.open(image) for image in images]
frame_one = frames[0]
frame_one.save(output_file, format="GIF", append_images=frames[1:],
save_all=True, duration=40, loop=0)

def convert_mp4_to_gif(input_file, output_file):
convert_mp4_to_jpgs(input_file)
convert_images_to_gif(output_file)

if __name__ == "__main__":
convert_mp4_to_gif("demo.mp4", "output.gif")

查看numpy版本小技巧

什么是pandas,利用pandas读取Excel文件

Windows和Linux环境下如何更换pip源?

Python包下载太慢?分享几个国内pip源

Python实现试卷的自动批改和自动评分功能代码示例

标签: MP4与GIF格式互转 Python

上面是“Python语法如何实现MP4与GIF格式互转(附代码)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_4346.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > 后端开发
本文共计844个字,预计阅读时长6分钟

基金从业资格考试题库

一站式备考基金从业资格考试,收录2021-2025年模拟题库!呱呱工具箱

AI工作站

收录全球3800+ 款各行各业AI应用,轻轻松松做事!

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
上一篇: 推荐一个可以查询老黄历的网站,可以根据五行取名!
下一篇: 详细介绍HTTP状态码不同之处的原因(什么原因导致的)
x 打工人ai神器