Created
February 19, 2013 07:38
-
-
Save carmensrz/4983816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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