从GIF中提取帧

use*_*125 6 macos applescript extract gif frame

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

谢谢!埃里克

小智 12

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

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

# convert 'animated.gif[0-10]' frames%03d.png
Run Code Online (Sandbox Code Playgroud)


Jus*_*tin 9

对于任何对使用原生预览应用程序导出单帧的方式感兴趣的人...

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

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

  • 这意味着如果你的 GIF 包含 100 帧,你将不得不手动命名 100 个文件,否则你最终会得到诸如“Untitiled-1 (dragged).tiff”之类的废话” (2认同)

jac*_*300 5

您可以使用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
Run Code Online (Sandbox Code Playgroud)

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