Skip to content

Instantly share code, notes, and snippets.

@carmensrz
Created February 19, 2013 07:38
Show Gist options
  • Save carmensrz/4983816 to your computer and use it in GitHub Desktop.
Save carmensrz/4983816 to your computer and use it in GitHub Desktop.
def bfs(img, origen, color):
pixeles = img.load()
width, height = img.size
cola = []
cola.append(origen)
original = pixeles[origen]
while len(cola)>0:
fila,columna = cola.pop(0)
actual = pixeles[fila,columna]
if actual == original or actual == color:
for dx in [-1,0,1]:
for dy in [-1,0,1]:
candidato = (fila + dy, columna + dx)
pixel_dy = candidato[0]
pixel_dx = candidato[1]
if pixel_dy >= 0 and pixel_dy < width and pixel_dx >= 0 and pixel_dx < height:
contenido = pixeles[pixel_dy,pixel_dx]
if contenido == original:
pixeles[pixel_dy,pixel_dx]=color
img.putpixel((pixel_dy,pixel_dx),color)
cola.append(candidato)
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment