Created
February 18, 2013 22:51
-
-
Save vane90/4981502 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
import sys, pygame | |
import Image | |
from math import* | |
from time import* | |
import math | |
import random | |
def im2(): | |
image = Image.open('binarizada1.png') | |
imagen = image.load() | |
ancho,alto = image.size | |
total = ancho*alto | |
porcentaje = [] | |
contiene = 0 | |
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): | |
color = random.randint(0,255),random.randint(0,255),random.randint(0,255) | |
n,xs,xy= bfs(image,color,a,b) | |
p = float(n)/float(total) * 100.0 | |
if p > 0.2: | |
porcentaje.append([p, (color)]) | |
contiene +=1 | |
fondo_new = porcentaje.index(max(porcentaje)) | |
max_c = porcentaje[fondo_new][1] | |
for i in range(ancho): | |
for j in range(alto): | |
if imagen[i,j]==max_c: | |
imagen[i,j]=(120,120,120) | |
nueva='fondogris.jpg' | |
image.save(nueva) | |
return nueva | |
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 | |
def main(): | |
pygame.init() # Inicializa pygame | |
screen = pygame.display.set_mode((400, 400)) | |
pygame.display.set_caption('Formas') | |
imagen = im2() | |
img = pygame.image.load(imagen) | |
screen = pygame.display.get_surface() | |
while True: # Ciclo para las acciones en la ventana | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
screen.blit(img, (0,0)) # muestra la posicion de la imagen en x = 0 y y=0 | |
pygame.display.update() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment