【问题标题】:Extracting frames from GIF从 GIF 中提取帧
【发布时间】:2014-12-22 10:01:16
【问题描述】:

我正在尝试使用 Applescript 或终端(或通过 applescript 使用 do shell script 命令的终端)自动从选定的 .gif 文件中提取帧。我知道如何使用 Preview.app 执行单击和拖动方法,但有没有办法自动执行此操作?

谢谢! 埃里克

【问题讨论】:

    标签: macos applescript frame extract gif


    【解决方案1】:

    ImageMagick 为您提供了一个 cmdlet,可以提取动画 gif 的帧。

    这将提取文件“animated.gif”的前 10 帧

    # convert 'animated.gif[0-10]' frames%03d.png
    

    【讨论】:

    • 有没有办法在不安装外部应用程序的情况下做到这一点?我正在考虑编写一个应用程序,您只需选择一个文件,一旦单击它,就会在指定位置生成 GIF 的帧
    • @user3541125 在动画 GIF 上,sip 只能转换第一帧
    【解决方案2】:

    对于任何对使用本机预览应用导出单个帧的方式感兴趣的人...

    1. 使用 OS X 原生预览应用打开 GIF
    2. 滚动到要提取的帧
    3. 单击图标(在左侧窗格中)并将其拖动到桌面

    http://www.macobserver.com/tmo/article/preview-extracting-frames-from-animated-gifs

    【讨论】:

    • 请注意,Preview 不能很好地处理大型 GIF,即 10+ MB 或数百帧。
    • 您可以使用 Mac App Store 中的 Gif Preview!
    • 这意味着如果你的 GIF 包含 100 帧,你将不得不手动命名 100 个文件,否则你最终会得到像“Untitiled-1 (dragged).tiff”这样的废话。 /跨度>
    【解决方案3】:

    您可以使用 AppKit 框架从 GIF 中提取帧。

    这是 AppleScript(在 Mavericks 上测试):

    set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF's files" with multiple selections allowed
    set dest to quoted form of POSIX path of (choose folder with prompt "Select the folder to save gif's frames")
    
    set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
    tName=os.path.basename(sys.argv[1])
    dir=sys.argv[2]
    app=NSApplication.sharedApplication() 
    img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
    if img:
         gifRep=img.representations()[0]
         frames=gifRep.valueForProperty_('NSImageFrameCount')
         if frames:
             for i in range(frames.intValue()):
                 gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
                 gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
             print (i + 1)"
    
    repeat with f in gifFiles
        set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer
    end repeat
    

    此脚本显示两个对话框窗口,一个选择 GIF 文件的对话框,一个选择目标文件夹的对话框。

    【讨论】:

    • 非常感谢!在我的应用程序完成之前,我还有最后一个问题。如何设置一个表示提取的 GIF 数量的变量(基本上是从该命令构建的)?
    • 查看更新后的答案,我将print (i + 1) 附加到 pScript
    • 这太棒了。优秀的答案!只是想指出,您可以将 NSGIFFileType 替换为 NSPNGFileType 或 NSJPEGFileType 以获取其他格式的导出文件。
    • 实际上,如果您在该脚本中将 NSGIFFileType 替换为 NSPNGFileType,它会发出错误:NameError: name 'NSPNGFileType' is not defined" number 1
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 2014-03-26
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2018-05-09
    • 2016-05-31
    相关资源
    最近更新 更多