• 图像转GIF和MP4


    # encoding:utf-8
    from py2neo import Graph
    from PIL import Image
    import os
    import moviepy.editor as mp
    import numpy as np
    import cv2
    import logging
    logging.basicConfig(level=logging.WARNING,
                        format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s:%(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    
    class img2mp4():
        #  图像转mp4, 思路是: picture -> gif -> mp4
        def __init__(self, imgdir):
            self.imgdir = imgdir  # 图像文件夹
    
        def mkGIF(self, fnames, gfile, delta, X=1920, Y=1080):
            #  生成gif的函数,原始图片仅支持png
            #  fnames: 图片
            #  gfile:生成的GIF文件名称
            #  delta: 每张图片之间的时间间隔
            frames = []
            order = 0
            for img in fnames:
                img = Image.open(img)
                width, height = img.size
                padx = (X - width) // 2
                pady = (Y - height) // 2
                arr = np.zeros((Y, X, 3), dtype=np.int8)
                arr[pady:Y - pady, padx:X - padx, :] = np.array(img)  # padding为统一大小
                amg = Image.fromarray(arr.astype("uint8"))
                frames.append(amg)
                order += 1
            frames[0].save(gfile, append_images=frames, save_all=1, duration=delta)
            return gfile
    
        def gif2Mp4(self, gif, mp4):
            # *.gif 转 *.mp4
            clip = mp.VideoFileClip(gif)
            clip.write_videofile(mp4)
    
        def pic2mp4(self, mp4):
            source = [os.path.join(self.imgdir, _)for _ in os.listdir(self.imgdir)]
            gname = os.path.join(os.path.dirname(mp4), f"{os.path.basename(mp4).split('.')[0]}.gif")
            gfile = self.createGIF(source, gname, duration=1/24)
            self.gif2Mp4(gfile, mp4)
    
    
    def video2pic(mp4, outdir):
        # 获得视频的格式
        videoCapture = cv2.VideoCapture(mp4)
        # 读帧
        success, frame = videoCapture.read()
        count = 1
        while success:
            img = Image.fromarray(frame)
            outname = os.path.join(outdir, f"{10000+count}.png")
            img.save(outname)
            logging.warning(f"Writing picture {outname}")
            count += 1
            success, frame = videoCapture.read()  # 下一帧
    
        videoCapture.release()
    

      

  • 相关阅读:
    用JS打开新窗口,防止被浏览器阻止的方法
    谷歌浏览器插件开发教程7
    谷歌浏览器插件开发教程6
    谷歌浏览器插件开发教程5
    谷歌浏览器插件开发教程4
    谷歌浏览器插件开发教程3
    谷歌浏览器插件开发教程2
    谷歌浏览器插件开发教程1
    从网站上偷图偷音乐偷视频教程
    pixijs shader 实现图片波浪效果
  • 原文地址:https://www.cnblogs.com/ddzhen/p/16112857.html
Copyright © 2020-2023  润新知