web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. AIGC工具
  5. 百度echarts
  6. 地图大全
  7. 前端知识
  8. 更多
    vuejs
    js入门
    php入门
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    AI应用

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

702 ℃

如何利用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")

AiPyApp:Python为核心的开源新AI助手,实现“知行合一”的智能协同

Keras:新手可用它5分钟搭出第一个神经网络

NumPy:一个Python生态系统中用于科学计算的核心库

查看numpy版本小技巧

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

标签: MP4与GIF格式互转 Python

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

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

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

当前位置: 首页 > 后端开发
Trae:新一代免费的AI编程工具

呱呱工具箱

一款免费的在线小工具,无需下载,打开即可使用!呱呱工具箱

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器