user3541125
user3541125

Reputation: 491

Extracting frames from GIF

I am trying to use Applescript or Terminal (or terminal via applescript using the do shell script command) to automatically extract the frames from a selected .gif file. I know how to do the click and drag method using Preview.app, but is there a way to do this automatically?

Thanks! Eric

Upvotes: 9

Views: 7698

Answers (3)

Justin
Justin

Reputation: 27301

For anyone interested in the way to export a single frame with the native Preview app...

  1. Open the GIF with OS X native Preview app
  2. Scroll to the frame you want to extract
  3. Click and drag the icon (in the left pane) to your desktop

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

Upvotes: 11

jackjr300
jackjr300

Reputation: 7191

You can use the AppKit framework to extract frames from GIF.

Here is the AppleScript (Tested on 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

This script show two dialog window, a dialog to choose GIF files, a dialog to choose the destination folder.

Upvotes: 5

deepb1ue
deepb1ue

Reputation: 161

ImageMagick gives you a cmdlet that lets extract frames of an animated gif.

This will extract the first 10 frames of the file "animated.gif"

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

Upvotes: 16

Related Questions