使用Python对GIF读取、生成、压缩和加水印

  • Post author:
  • Post category:python




环境说明

Python环境下对GIF文件进行读取、生成、压缩和加水印,主要使用到的包有:

cv2
numpy
imageio
pygifsicle
ffmpeg



读取

__author__ = "TracelessLe"

import cv2
import os
import numpy as np

class GIF():
    def __init__(self, file_path):
        self.file_path = file_path
        self.material = []

    def load(self):
        cap = cv2.VideoCapture(self.file_path)
        self.frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        self.frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        self.fps = cap.get(cv2.CAP_PROP_FPS)
        res = []
        while True:
            ret, frame = cap.read()
            if ret is False:
                break
            res.append(frame)
        self.material = res
        self.total_frames = len(res)
        cap.release()
        return self.material

    def get_durations(self):
        if self.fps is None:
            return 0.125
        return 1/self.fps


if __name__ == '__main__':
    gif_path