Skip to content

Instantly share code, notes, and snippets.

@pepgonzalez
Created May 9, 2013 16:09
Show Gist options
  • Save pepgonzalez/5548453 to your computer and use it in GitHub Desktop.
Save pepgonzalez/5548453 to your computer and use it in GitHub Desktop.
from Tkinter import *
from sys import argv
from PIL import Image, ImageTk
from datetime import *
import random as R
def filtroGrisesPromedio(imagen):
x, y = imagen.size
px = imagen.load()
imagenGrises = Image.new('RGB',(x,y))
for i in range(x):
for j in range(y):
pixeles = px[i,j]
prom = sum(pixeles) / 3
imagenGrises.putpixel((i,j),(prom,prom,prom))
return imagenGrises
def bordesPorResta(im):
x,y = im.size
m1 = im.load()
for i in range(5):
imagen2 = FiltroMediano(im)
m2 = imagen2.load()
imb = Image.new('RGB',(x,y))
for i in range(x):
for j in range(y):
r = m1[i,j][0] - m2[i,j][0]
g = m1[i,j][1] - m2[i,j][1]
b = m1[i,j][2] - m2[i,j][2]
pixel = (r,g,b)
imb.putpixel((i,j),pixel)
return imb
def FiltroMediano(imagen):
im = imagen
ancho, altura = imagen.size
p = imagen.load()
for i in range(ancho):
for j in range(altura):
lp = []
for k in range(-1,2):
for l in range(-1,2):
if i+k >= 0 and j+l >= 0 and i+k < ancho and j+l < altura:
lp.append(p[i+k,j+l][1])
lp.sort()
if len(lp) % 2 != 0:
valor = lp[(len(lp)/2)]
else:
valor = (lp[(len(lp)/2) - 1] + lp[(len(lp)/2)])/2
p[i,j] = (valor,valor,valor)
return im
def filtroPromedio(imagen):
x,y = imagen.size
pixeles = imagen.load()
imagenFiltrada = Image.new('RGB',(x,y))
c = 1
for i in range(x):
for j in range(y):
px = pixeles[i,j]
lista = list()
try:
p1 = pixeles[i+1,j]
c += 1
lista.append(p1)
except:
p1 = (0,0,0)
lista.append(p1)
try:
p2 = pixeles[i-1,j]
c += 1
lista.append(p2)
except:
p2 = (0,0,0)
lista.append(p2)
try:
p3 = pixeles[i,j+1]
c += 1
lista.append(p3)
except:
p3 = (0,0,0)
lista.append(p3)
try:
p4 = pixeles[i,j-1]
c += 1
lista.append(p4)
except:
p4 = (0,0,0)
lista.append(p4)
print lista
print "ordenada"
print lista.sort()
return imagenFiltrada
def main():
p = "test.png"
im = Image.open(p)
gris = filtroGrisesPromedio(im)
i_f = FiltroMediano(gris)
for i in range (5):
i_f = FiltroMediano(i_f)
f = bordesPorResta(gris)
f.show()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment