Skip to content

Instantly share code, notes, and snippets.

@vane90
Created February 19, 2013 06:31
def im2():
image = Image.open('binarizada1.png')
imagen = image.load()
ancho,alto = image.size
print ancho
print alto
for a in range(ancho):
for b in range(alto):
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
if imagen[a,b]==(0,0,0):
n,xs,xy= bfs(image,color,a,b)
nueva='formas.jpg'
image.save(nueva)
return nueva
#aplicamos bfs en la imagen
def bfs(image,color,a,b):
imagen=image.load()
ancho,alto=image.size
original = imagen[a,b]
c=[] #lista color
xs=[] #coordenada x
ys=[] #coordenada y
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