从GIF中提取帧

9
我正在尝试使用Applescript或终端(或通过使用do shell script命令的终端来使用Applescript)自动从所选的.gif文件中提取帧。我知道如何使用Preview.app使用点击和拖动方法,但有没有自动执行此操作的方法?
谢谢! Eric
3个回答

14
ImageMagick提供了一个命令行工具,可以提取动态gif图像的帧。 这将提取文件“animated.gif”的前10帧。
# convert 'animated.gif[0-10]' frames%03d.png

有没有一种方法可以不安装外部应用程序就能实现这个功能?我正在考虑编写一个应用程序,在其中您只需选择一个文件,然后点击它,GIF的帧将在指定位置生成。 - user3541125
1
@user3541125 在动态GIF上,sips只能转换第一帧。 - jackjr300

10

请注意,预览无法很好地处理大型GIF,即10+ MB或数百帧。 - Taylor D. Edmiston
你可以在Mac App Store中使用Gif预览! - Khaaba
2
这意味着如果您的GIF包含100帧,您将不得不手动命名100个文件,否则您最终会得到像“Untitiled-1(dragged).tiff”这样的无意义文件名。 - pookie

5
你可以使用AppKit框架从GIF中提取帧。
以下是苹果脚本(在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 数量(基本上是在那个命令的基础上构建)? - user3541125
请查看更新后的答案,我在pScript中添加了print (i + 1) - jackjr300
这太棒了。回答真是精彩!只想指出你可以将NSGIFFileType替换为NSPNGFileType或NSJPEGFileType以获得其他格式的导出文件。 - Jeremy Brooks
1
实际上,如果您在该脚本中将NSGIFFileType替换为NSPNGFileType,则会发出错误:NameError:未定义名称'NSPNGFileType'”编号1 - Erika Electra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接