Skip to content

Instantly share code, notes, and snippets.

@vane90
Created February 18, 2013 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vane90/4981271 to your computer and use it in GitHub Desktop.
Save vane90/4981271 to your computer and use it in GitHub Desktop.
def bfs(image,color,a,b):
imagen=image.load()
ancho,alto=image.size
original = imagen[a,b]
c=[]
xs=[]
ys=[]
c.append((a,b))
n = 0
while len(c) > 0:
(x, y) = c.pop(0)
actual = imagen[x, y]
if actual == original or actual == color:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
i, j = (x + dy, y + dx)
if i >= 0 and i < ancho and j >= 0 and j < alto:
contiene = imagen[i, j]
if contiene == original:
imagen[i, j] = color
xs.append(i)
ys.append(j)
n += 1
c.append((i, j))
forma1 = image.save('formas.png')
return n, xs, ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment