Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An class to extract the time information and layers from the images generated by the PSScriptExporter
class ImportFromGif( object ):
def __init__( self ):
pass
def extractComponentsFromName( self, fullPathFileName ):
name = os.path.basename( fullPathFileName )
comps = name.split( '__' )
( dirnames, filename ) = os.path.split( fullPathFileName )
dirnames = dirnames.split( os.path.sep )
res = []
for c in comps:
if len( c.strip() ) > 0:
res.append( c.strip() )
cnt = len( res )
if cnt == 0:
return None
elif cnt > 3:
joined = res[2:].join( '_' )
res = res[:2]
res.append( joined )
while len( res ) < 3:
if len( dirnames ) > 1:
infer = dirnames.pop()
else:
infer = res[0]
res.insert( 0, infer )
return res
def extractTimes( self, fullPathFileName ):
times = []
with open( fullPathFileName, 'rb' ) as f:
gif = Image.open( f )
( self.height, self.width ) = gif.size
times.append( gif.info[ 'duration' ] )
while True:
try:
gif.seek( gif.tell() + 1 )
times.append( gif.info[ 'duration' ] )
except EOFError as e:
# gif.close()
break
return times
def importFromGif( self, gifFileName, outDir, outFile ):
absGif = os.path.abspath( gifFileName );
( gifDir, name ) = os.path.split( gifFileName )
( nonext, ext ) = os.path.splitext( absGif )
baseName = os.path.basename( nonext );
comps = self.extractComponentsFromName( baseName )
times = self.extractTimes( absGif )
fnodes = []
for x in range( len( times ) ):
fr = { "bpps" : [],
"height" : self.height,
"width" : self.width,
"frameMilliseconds" : times[ x ],
"name" : "Frame_" + str( x ),
"nodes" : [] }
for y in range( 10 ):
fbase = nonext + "_" + str( x ) + "_ly" + str( y ) + "_.png"
if os.path.exists( fbase ):
while len( fr[ "nodes" ] ) < y:
# create missing layers as empty layers
fr[ "nodes" ].append( { "imageName" : "",
"name" : "Layer_" + str( len( fr[ "nodes" ] ) ),
"flipHorizontal" : False } )
fr[ "nodes" ].append( { "flipHorizontal" : False,
"flipVertical" : False,
"height" : self.height,
"width" : self.width,
"imageName" : fbase[ len( gifDir) + 1: ],
"name" : "Layer_" + str( y ),
"rotation" : 0 } )
fnodes.append( fr )
res = { "imageDirectories" : [ gifDir ],
"sprites" : [
{ "name" : comps[0],
"nodes" : [
{ "name" : comps[1],
"nodes" : [
{ "name" : comps[2],
"defaultMillis" : 50,
"height" : self.height,
"width" : self.width,
"nodes" : fnodes }
]
}
]
}
]
}
outf = open( os.path.join( outDir, outFile), 'w' )
json.dump( res, outf )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.