Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
44
Task/Bitmap-Flood-fill/Python/bitmap-flood-fill-1.py
Normal file
44
Task/Bitmap-Flood-fill/Python/bitmap-flood-fill-1.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import Image
|
||||
def FloodFill( fileName, initNode, targetColor, replaceColor ):
|
||||
img = Image.open( fileName )
|
||||
pix = img.load()
|
||||
xsize, ysize = img.size
|
||||
Q = []
|
||||
if pix[ initNode[0], initNode[1] ] != targetColor:
|
||||
return img
|
||||
Q.append( initNode )
|
||||
while Q != []:
|
||||
node = Q.pop(0)
|
||||
if pix[ node[0], node[1] ] == targetColor:
|
||||
W = list( node )
|
||||
if node[0] + 1 < xsize:
|
||||
E = list( [ node[0] + 1, node[1] ] )
|
||||
else:
|
||||
E = list( node )
|
||||
# Move west until color of node does not match targetColor
|
||||
while pix[ W[0], W[1] ] == targetColor:
|
||||
pix[ W[0], W[1] ] = replaceColor
|
||||
if W[1] + 1 < ysize:
|
||||
if pix[ W[0], W[1] + 1 ] == targetColor:
|
||||
Q.append( [ W[0], W[1] + 1 ] )
|
||||
if W[1] - 1 >= 0:
|
||||
if pix[ W[0], W[1] - 1 ] == targetColor:
|
||||
Q.append( [ W[0], W[1] - 1 ] )
|
||||
if W[0] - 1 >= 0:
|
||||
W[0] = W[0] - 1
|
||||
else:
|
||||
break
|
||||
# Move east until color of node does not match targetColor
|
||||
while pix[ E[0], E[1] ] == targetColor:
|
||||
pix[ E[0], E[1] ] = replaceColor
|
||||
if E[1] + 1 < ysize:
|
||||
if pix[ E[0], E[1] + 1 ] == targetColor:
|
||||
Q.append( [ E[0], E[1] + 1 ] )
|
||||
if E[1] - 1 >= 0:
|
||||
if pix[ E[0], E[1] - 1 ] == targetColor:
|
||||
Q.append( [ E[0], E[1] -1 ] )
|
||||
if E[0] + 1 < xsize:
|
||||
E[0] = E[0] + 1
|
||||
else:
|
||||
break
|
||||
return img
|
||||
Loading…
Add table
Add a link
Reference in a new issue