【问题标题】:How to create gifs with images in Python [closed]如何在 Python 中创建带有图像的 gif [关闭]
【发布时间】:2016-07-22 00:05:57
【问题描述】:

是否有任何 API 或库可供我使用 .jpgs 和 .pngs 创建 .gif?我正在寻找一个可以与 Python 3 一起使用的。

【问题讨论】:

    标签: python


    【解决方案1】:

    这是我不久前写的东西。当我在过去六个月写这篇文章时,我找不到更好的东西。希望这可以让你开始。它使用了 here! 找到的 ImageMagick

    import os, sys
    import glob
    dataDir = 'directory of image files' #must contain only image files
    #change directory gif directory
    os.chdir(dataDir)
    
    
    #Create txt file for gif command
    fileList = glob.glob('*') #star grabs everything, can change to *.png for only png files
    fileList.sort()
    #writes txt file
    file = open('blob_fileList.txt', 'w')
    for item in fileList:
        file.write("%s\n" % item)
    
    file.close()
    
    
    #verifies correct convert command is being used, then converts gif and png to new gif
    os.system('SETLOCAL EnableDelayedExpansion')
    os.system('SET IMCONV="C:\Program Files\ImageMagick-6.9.1-Q16\Convert"')
    # The following line is where you can set delay of images (100)
    os.system('%IMCONV% -delay 100 @fileList.txt theblob.gif')
    

    新的转换通话

    无论出于何种原因(我没有查看差异),我不再需要代码中的 set local 和 set 命令行。我以前需要它,因为即使设置了路径也没有使用转换命令。当您使用新的 7.. 版本的图像魔法时。请参阅新的代码行以替换最后三行。 请务必选中要求下载旧命令的复选框,例如安装时转换。一切都被转换成一个单一的魔法命令,但我不想重新学习新的约定。

    os.system('convert -delay 50 @fileList.txt animated.gif')
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2015-11-21
      • 2013-05-15
      • 2013-03-26
      • 2015-04-23
      • 1970-01-01
      • 2020-02-20
      • 2011-10-19
      • 2014-10-04
      • 2011-06-10
      相关资源
      最近更新 更多