Created
May 16, 2013 14:49
This file contains hidden or 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
#!/usr/bin/python | |
import math | |
import sys | |
import filtros | |
import random | |
from math import sqrt, atan2, pi, atan | |
from collections import defaultdict | |
from PIL import ImageDraw | |
import Image | |
import glob | |
import os | |
import time | |
def frames(): | |
"""toma todas las imagenes de | |
la carpeta | |
""" | |
os.chdir("cuatro") | |
files = [] | |
secuencia = [] | |
for file in glob.glob("*.png"): | |
files.append(file) | |
for i in range(1, len(files)): | |
secuencia.append("%04d.png" %i) | |
print secuencia | |
return secuencia | |
def bfs(imagen, origen, color): | |
"""colorea todo el objeto recibe como parametros el | |
nuevo color con el que se pinta,la coordenada de inicio y | |
la imagen, y regresa un arreglo con la masa y la imagen | |
""" | |
cola = [] | |
cont = 0 | |
masa = [] | |
pixeles = imagen.load() | |
alto, ancho = imagen.size | |
cola.append(origen) | |
original = pixeles[origen] | |
todos_pixeles = [] | |
while len(cola) > 0: | |
(x, y) = cola.pop(0) | |
actual = pixeles[x, y] | |
if actual == original or actual == color: | |
for dx in [-1, 0, 1]: | |
for dy in [-1, 0, 1]: | |
candidato = (x + dx, y + dy) | |
pix_x = candidato[0] | |
pix_y = candidato[1] | |
if pix_x >= 0 and pix_x < alto and pix_y >= 0 and pix_y < ancho: | |
contenido = pixeles[pix_x, pix_y] | |
if contenido == original: | |
pixeles[pix_x, pix_y] = color | |
masa.append((pix_x,pix_y)) | |
imagen.putpixel((pix_x, pix_y), color) | |
cont += 1 | |
cola.append((pix_x, pix_y)) | |
todos_pixeles.append((pix_x, pix_y)) | |
imagen.save("../bfs"+str(time.time()), 'png') | |
return imagen, cont, masa, todos_pixeles | |
def diferencia(ant, act): | |
""" | |
""" | |
alto, ancho = ant.size | |
p_ant = ant.load() | |
p_act = act.load() | |
nueva = Image.new("RGB", (alto, ancho)) | |
for i in range(alto): | |
for j in range(ancho): | |
if abs(p_ant[i,j][0]-p_act[i,j][0])>4: | |
a = abs(p_ant[i,j][0]-p_act[i,j][0]) | |
color = (0,0,0) | |
else: | |
color = (255,255,255) | |
nueva.putpixel((i,j), color) | |
nueva.save("../dif/"+str(time.time())+".png") | |
return nueva | |
def main(): | |
existe = False | |
secuencia = frames() | |
for i in range(len(secuencia)): | |
if i == 0: | |
ant = filtros.abrir_imagen(secuencia[i]) | |
act = filtros.abrir_imagen(secuencia[i]) | |
continue | |
ant = act | |
act = filtros.abrir_imagen(secuencia[i]) | |
aux = act.copy() | |
draw = ImageDraw.Draw(aux) | |
res = diferencia(ant, act) | |
alto, ancho = res.size | |
pixeles = res.load() | |
for x in range(alto): | |
for y in range(ancho): | |
if pixeles[x,y] == (0,0,0): | |
r = int(random.random()*250) | |
g = int(random.random()*250) | |
b = int(random.random()*250) | |
nuevo_color = (r,g,b) | |
imagen, cont, masa, todos_pixeles = bfs(res, (x,y), nuevo_color) | |
x_max = todos_pixeles[0][0] | |
x_min = todos_pixeles[0][0] | |
y_max = todos_pixeles[0][1] | |
y_min = todos_pixeles[0][1] | |
total_x = 0 | |
total_y = 0 | |
for l in range(len(masa)): | |
total_x = total_x + masa[l][0] | |
total_y = total_y + masa[l][1] | |
x_centro = total_x/len(masa) | |
y_centro = total_y/len(masa) | |
centro = (x_centro,y_centro) | |
for p in todos_pixeles: | |
if p[0] > x_max: | |
x_max = p[0] | |
if p[0] < x_min: | |
x_min = p[0] | |
if p[1] > y_max: | |
y_max = p[1] | |
if p[1] < y_min: | |
y_min = p[1] | |
if abs(y_max-y_min) > abs(x_max-x_min): | |
print "mov vertical" | |
if existe: | |
if pp[x_max+1, y_max-3] == (255, 255, 255): | |
print "mov izq" | |
draw.text(centro, "->", (255, 0, 0)) | |
else: | |
print "mov der" | |
draw.text(centro, "<-", (255, 0, 0)) | |
aux.save("../fin/"+str(time.time())+".png") | |
else: | |
print "mov horizontal" | |
if existe: | |
if pp[x_max-3, y_max+1] == (255, 255, 255): | |
print "mov abajo" | |
draw.text(centro, "v", (255, 0, 0)) | |
else: | |
print "mov arriba" | |
draw.text(centro, "^", (255, 0, 0)) | |
aux.save("../fin/"+str(time.time())+".png") | |
try: | |
a = res.copy() | |
pp = a.load() | |
existe = True | |
except: | |
pass | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment