#NEWLINE# def _locateAll_python( needleImage , haystackImage , grayscale = None , limit = None , region = None , step = 1 ) : #NEWLINE# #INDENT# if ( grayscale is None ) : #NEWLINE# #INDENT# grayscale = GRAYSCALE_DEFAULT #NEWLINE# #UNINDENT# needleFileObj = None #NEWLINE# if isinstance ( needleImage , str ) : #NEWLINE# #INDENT# needleFileObj = open ( needleImage , 'rb' ) #NEWLINE# needleImage = Image . open ( needleFileObj ) #NEWLINE# #UNINDENT# haystackFileObj = None #NEWLINE# if isinstance ( haystackImage , str ) : #NEWLINE# #INDENT# haystackFileObj = open ( haystackImage , 'rb' ) #NEWLINE# haystackImage = Image . open ( haystackFileObj ) #NEWLINE# #UNINDENT# if ( region is not None ) : #NEWLINE# #INDENT# haystackImage = haystackImage . crop ( ( region [ 0 ] , region [ 1 ] , ( region [ 0 ] + region [ 2 ] ) , ( region [ 1 ] + region [ 3 ] ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# region = ( 0 , 0 ) #NEWLINE# #UNINDENT# if grayscale : #NEWLINE# #INDENT# needleImage = ImageOps . grayscale ( needleImage ) #NEWLINE# haystackImage = ImageOps . grayscale ( haystackImage ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( needleImage . mode == 'RGBA' ) : #NEWLINE# #INDENT# needleImage = needleImage . convert ( 'RGB' ) #NEWLINE# #UNINDENT# if ( haystackImage . mode == 'RGBA' ) : #NEWLINE# #INDENT# haystackImage = haystackImage . convert ( 'RGB' ) #NEWLINE# #UNINDENT# #UNINDENT# ( needleWidth , needleHeight ) = needleImage . size #NEWLINE# ( haystackWidth , haystackHeight ) = haystackImage . size #NEWLINE# needleImageData = tuple ( needleImage . getdata ( ) ) #NEWLINE# haystackImageData = tuple ( haystackImage . getdata ( ) ) #NEWLINE# needleImageRows = [ needleImageData [ ( y * needleWidth ) : ( ( y + 1 ) * needleWidth ) ] for y in range ( needleHeight ) ] #NEWLINE# needleImageFirstRow = needleImageRows [ 0 ] #NEWLINE# assert ( len ( needleImageFirstRow ) == needleWidth ) , 'For some reason, the calculated width of first row of the needle image is not the same as the width of the image.' #NEWLINE# assert ( [ len ( row ) for row in needleImageRows ] == ( [ needleWidth ] * needleHeight ) ) , "For some reason, the needleImageRows aren't the same size as the original image." #NEWLINE# numMatchesFound = 0 #NEWLINE# firstFindFunc = _kmp #NEWLINE# step = 1 #NEWLINE# for y in range ( haystackHeight ) : #NEWLINE# #INDENT# for matchx in firstFindFunc ( needleImageFirstRow , haystackImageData [ ( y * haystackWidth ) : ( ( y + 1 ) * haystackWidth ) ] , step ) : #NEWLINE# #INDENT# foundMatch = True #NEWLINE# for searchy in range ( 1 , needleHeight , step ) : #NEWLINE# #INDENT# haystackStart = ( ( ( searchy + y ) * haystackWidth ) + matchx ) #NEWLINE# if ( needleImageData [ ( searchy * needleWidth ) : ( ( searchy + 1 ) * needleWidth ) ] != haystackImageData [ haystackStart : ( haystackStart + needleWidth ) ] ) : #NEWLINE# #INDENT# foundMatch = False #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# if foundMatch : #NEWLINE# #INDENT# numMatchesFound += 1 #NEWLINE# ( yield ( ( matchx + region [ 0 ] ) , ( y + region [ 1 ] ) , needleWidth , needleHeight ) ) #NEWLINE# if ( ( limit is not None ) and ( numMatchesFound >= limit ) ) : #NEWLINE# #INDENT# if ( needleFileObj is not None ) : #NEWLINE# #INDENT# needleFileObj . close ( ) #NEWLINE# #UNINDENT# if ( haystackFileObj is not None ) : #NEWLINE# #INDENT# haystackFileObj . close ( ) #NEWLINE# #UNINDENT# raise StopIteration ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( needleFileObj is not None ) : #NEWLINE# #INDENT# needleFileObj . close ( ) #NEWLINE# #UNINDENT# if ( haystackFileObj is not None ) : #NEWLINE# #INDENT# haystackFileObj . close ( ) #NEWLINE# #UNINDENT# if ( RAISE_IF_NOT_FOUND and ( numMatchesFound == 0 ) ) : #NEWLINE# #INDENT# raise ImageNotFoundException ( 'Could not locate the image.' )