#NEWLINE# @ staticmethod #NEWLINE# def quantize( pixels , max_color ) : #NEWLINE# #INDENT# 'Quantize.\n\n        :param pixels: a list of pixel in the form (r, g, b)\n        :param max_color: max number of colors\n        ' #NEWLINE# if ( not pixels ) : #NEWLINE# #INDENT# raise Exception ( 'Empty pixels when quantize.' ) #NEWLINE# #UNINDENT# if ( ( max_color < 2 ) or ( max_color > 256 ) ) : #NEWLINE# #INDENT# raise Exception ( 'Wrong number of max colors when quantize.' ) #NEWLINE# #UNINDENT# histo = MMCQ . get_histo ( pixels ) #NEWLINE# if ( len ( histo ) <= max_color ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# vbox = MMCQ . vbox_from_pixels ( pixels , histo ) #NEWLINE# pq = PQueue ( ( lambda x : x . count ) ) #NEWLINE# pq . push ( vbox ) #NEWLINE# def iter_( lh , target ) : #NEWLINE# #INDENT# n_color = 1 #NEWLINE# n_iter = 0 #NEWLINE# while ( n_iter < MMCQ . MAX_ITERATION ) : #NEWLINE# #INDENT# vbox = lh . pop ( ) #NEWLINE# if ( not vbox . count ) : #NEWLINE# #INDENT# lh . push ( vbox ) #NEWLINE# n_iter += 1 #NEWLINE# continue #NEWLINE# #UNINDENT# ( vbox1 , vbox2 ) = MMCQ . median_cut_apply ( histo , vbox ) #NEWLINE# if ( not vbox1 ) : #NEWLINE# #INDENT# raise Exception ( "vbox1 not defined; shouldn't happen!" ) #NEWLINE# #UNINDENT# lh . push ( vbox1 ) #NEWLINE# if vbox2 : #NEWLINE# #INDENT# lh . push ( vbox2 ) #NEWLINE# n_color += 1 #NEWLINE# #UNINDENT# if ( n_color >= target ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if ( n_iter > MMCQ . MAX_ITERATION ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# n_iter += 1 #NEWLINE# #UNINDENT# #UNINDENT# iter_ ( pq , ( MMCQ . FRACT_BY_POPULATIONS * max_color ) ) #NEWLINE# pq2 = PQueue ( ( lambda x : ( x . count * x . volume ) ) ) #NEWLINE# while pq . size ( ) : #NEWLINE# #INDENT# pq2 . push ( pq . pop ( ) ) #NEWLINE# #UNINDENT# iter_ ( pq2 , ( max_color - pq2 . size ( ) ) ) #NEWLINE# cmap = CMap ( ) #NEWLINE# while pq2 . size ( ) : #NEWLINE# #INDENT# cmap . push ( pq2 . pop ( ) ) #NEWLINE# #UNINDENT# return pq