#NEWLINE# def Atlas( ims , outname , max_size = 2048 , scale_factor = 1 , ignore_exceptions = False , minimize_num_textures = True , force_square = False ) : #NEWLINE# #INDENT# blocksize = 4 #NEWLINE# dim = GetDim ( ims , blocksize , max_size , scale_factor ) #NEWLINE# size = ( dim , dim ) #NEWLINE# ims = sorted ( ims , key = ( lambda im : ( im . size [ 1 ] * im . size [ 0 ] ) ) , reverse = True ) #NEWLINE# fullboxes = { #NEWLINE# #INDENT# 0 : ( size , [ ] ) , #NEWLINE# #UNINDENT# } #NEWLINE# source_idx = 0 #NEWLINE# def LocalAtlas( im , source_idx ) : #NEWLINE# #INDENT# if ( ( im . size [ 0 ] > size [ 0 ] ) or ( im . size [ 1 ] > size [ 1 ] ) ) : #NEWLINE# #INDENT# sys . stderr . write ( ( ( 'Error: image ' + im . name ) + ' is larger than the atlas size!\n' ) ) #NEWLINE# sys . exit ( 2 ) #NEWLINE# #UNINDENT# inserted = False #NEWLINE# for ( idx , fb ) in fullboxes . items ( ) : #NEWLINE# #INDENT# atlassize = dim #NEWLINE# fblist = fb [ 1 ] #NEWLINE# insertbbox = TryInsertImage ( im . size [ 0 ] , im . size [ 1 ] , fblist , atlassize ) #NEWLINE# if insertbbox : #NEWLINE# #INDENT# inserted = True #NEWLINE# fblist . append ( FullBox ( im , idx , insertbbox , im . name ) ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# if ( not inserted ) : #NEWLINE# #INDENT# numoutimages = len ( fullboxes ) #NEWLINE# newsize = GetDim ( ims [ source_idx : ] , blocksize , max_size , scale_factor ) #NEWLINE# fullboxes [ numoutimages ] = ( ( newsize , newsize ) , [ FullBox ( im , numoutimages , BBox ( 0 , 0 , im . size [ 0 ] , im . size [ 1 ] ) , im . name ) ] ) #NEWLINE# #UNINDENT# return ( source_idx + 1 ) #NEWLINE# #UNINDENT# if ignore_exceptions : #NEWLINE# #INDENT# for im in ims : #NEWLINE# #INDENT# source_idx = LocalAtlas ( im , source_idx ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'Atlasing' ) #NEWLINE# for im in ims : #NEWLINE# #INDENT# source_idx = LocalAtlas ( im , source_idx ) #NEWLINE# #UNINDENT# #UNINDENT# atlases = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( idx , fb ) in fullboxes . items ( ) : #NEWLINE# #INDENT# w = int ( fb [ 0 ] [ 0 ] ) #NEWLINE# h = int ( fb [ 0 ] [ 1 ] ) #NEWLINE# if ( not force_square ) : #NEWLINE# #INDENT# sz = fb [ 0 ] [ 0 ] #NEWLINE# fblist = fb [ 1 ] #NEWLINE# maxy = 0 #NEWLINE# maxx = 0 #NEWLINE# for b in fblist : #NEWLINE# #INDENT# fbmaxy = ( b . bbox . y + b . bbox . h ) #NEWLINE# fbmaxx = ( b . bbox . x + b . bbox . w ) #NEWLINE# maxy = max ( maxy , fbmaxy ) #NEWLINE# maxx = max ( maxx , fbmaxx ) #NEWLINE# #UNINDENT# if ( maxy <= ( h / 2 ) ) : #NEWLINE# #INDENT# h = int ( ( h / 2 ) ) #NEWLINE# #UNINDENT# if ( maxx <= ( w / 2 ) ) : #NEWLINE# #INDENT# w = int ( ( fbmaxy / 2 ) ) #NEWLINE# #UNINDENT# #UNINDENT# mips = [ ] #NEWLINE# divisor = 1 #NEWLINE# contained_images = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# while ( ( w >= 1 ) or ( h >= 1 ) ) : #NEWLINE# #INDENT# outim = OutImage ( '{0}-{1}' . format ( outname , idx ) , Image . new ( 'RGBA' , ( w , h ) ) ) #NEWLINE# for b in fb [ 1 ] : #NEWLINE# #INDENT# ( b_w , b_h ) = b . im . size #NEWLINE# ( b_w , b_h ) = ( ( b_w / divisor ) , ( b_h / divisor ) ) #NEWLINE# if ( ( b_w > 0 ) and ( b_h > 0 ) ) : #NEWLINE# #INDENT# resized_b = b . im . resize ( ( b_w , b_h ) , Image . ANTIALIAS ) #NEWLINE# ( b_x , b_y ) = ( ( b . bbox . x / divisor ) , ( b . bbox . y / divisor ) ) #NEWLINE# outim [ 1 ] . paste ( resized_b , ( b_x , b_y ) ) #NEWLINE# contained_images [ b . im ] = True #NEWLINE# #UNINDENT# #UNINDENT# mips . append ( outim ) #NEWLINE# divisor = ( divisor << 1 ) #NEWLINE# if ( ( w == 1 ) and ( h == 1 ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# w = max ( 1 , ( w >> 1 ) ) #NEWLINE# h = max ( 1 , ( h >> 1 ) ) #NEWLINE# #UNINDENT# bboxes = { b . name : b . bbox for b in fb [ 1 ] } #NEWLINE# atlases [ idx ] = AtlasData ( contained_images . keys ( ) , mips , bboxes ) #NEWLINE# #UNINDENT# return atlases