Split/Export Gifs into individual PNG,GIF or JPG files

(Mark Hunte) #1

The other day I was looking at splitting gifs so I could test a png loader.

I am sure I had a easy way of doing this … once but for the life of me cannot remember how I did it before…

So just looking around www, I came across a stackoverflow answer that does this.

I have changed the script to be a bit more useful, i.e

•name your exported files or just have numbers.
•Multiple export types.

The script is run via bridged Applescript/Objective-C/python. ( got to love bridging in Script Editor )

To use just put this code in a new Script Editor - AppleScript doc and run.
You can save the Doc. Note do a compile first and then save.

       use framework "Foundation"
use scripting additions

set chooseExportTypeObjc to {png:"NSPNGFileType", gif:"NSGIFFileType", jpg:"NSJPEGFileType"}

set objCDictionary to current application's NSDictionary's dictionaryWithDictionary:chooseExportTypeObjc

set allKeys to objCDictionary's allKeys()
set allValues to objCDictionary's allValues()

set the importTypeString to allValues's componentsJoinedByString:","

set chosenExportType to item 1 of (choose from list (allKeys as list) with prompt "Select your Export Type:" default items {"png"})

set chosenExportTypeObj to (objCDictionary's valueForKey:(chosenExportType)) as text

set gifFiles to {choose file of type "com.compuserve.gif" with prompt "Select GIF's files" without multiple selections allowed}

set dest to quoted form of POSIX path of (choose folder with prompt "Select the folder to save gif's frames")

display dialog "Leave blank if you just want numbers" default answer "" buttons {"OK"} default button 1
copy the result as list to {button_pressed, text_returned}

if text_returned is not equal to "" then
	text_returned = text_returned & space
end if

set pScript to ("from AppKit import NSApplication, NSImage, NSImageCurrentFrame," & importTypeString as text) & " ; import sys, os
nname=    str('" & text_returned & "') 
if nname:
	nname= nname + '_'

if img:
     if frames:
         for i in range(frames.intValue()):
             gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
             gifRep.representationUsingType_properties_(" & chosenExportTypeObj & ", None).writeToFile_atomically_(dir + nname + str(i + 1).zfill(2) + '." & chosenExportType & "', True)
         print (i + 1)"

set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & (quoted form of pScript) & " " & (quoted form of POSIX path of item 1 of gifFiles) & " " & dest) as integer

Choose your export file type. (png, gif,jpg)

Choose your gif file.

Choose your destination for the exported frames.

Set your exported file names or just hit OK with out entering a name to just get file numbers.

If you are familiar with Applescript you can make it into an app etc…

(Mark Hunte) #2

I have updated the above for multiple export types.


Thank You Mark!

If you are familiar with Applescript you can make it into an app etc…

I took the liberty of creating an app with your code in case there are people who are not familiar with doing it themselves.

GIF_Split.app.zip (54.3 KB)

Open the “GIF_Split” app then, as per Mark’s instructions…