Skip to content

Instantly share code, notes, and snippets.

@pepgonzalez
Last active June 2, 2016 06:01
Show Gist options
  • Save pepgonzalez/4759081 to your computer and use it in GitHub Desktop.
Save pepgonzalez/4759081 to your computer and use it in GitHub Desktop.
def filtroConvolucion(imagen):
x,y = imagen.size
px = imagen.load()
#MX = [[-1,0,1],[-1,0,1],[-1,0,1]] #mascara lineas horizontales
MX = [[-1,0,1],[-2,0,2],[-1,0,1]]
#MY = [[1,1,1],[0,0,0],[-1,-1,-1]] #mascara lineas verticales
MY = [[1,2,1],[0,0,0],[-1,-2,-1]]
imagenNuevaX = Image.new('RGB',(x,y))
imagenNuevaY = Image.new('RGB',(x,y))
imn = Image.new('RGB',(x,y))
for j in range(y):
for i in range(x):
sumatoria = 0
sumatoriay = 0
for mj in range(-1,2):
for mx in range(-1,2):
try:
sumatoria += MX[mj+1][mx+1]*px[i+mx,j+mj][1]
sumatoriay += MY[mj+1][mx+1]*px[i+mx,j+mj][1]
except:
sumatoria += 0
sumatoriay += 0
punto1 = sumatoria
punto2 = sumatoriay
#Normalizar
if(punto1 < 0):
punto1 = 0
if(punto1 > 255):
punto1 = 255
if(punto2 < 0):
punto2 = 0
if(punto2 > 255):
punto2 = 255
imagenNuevaX.putpixel((i,j),(punto1,punto1,punto1))
imagenNuevaY.putpixel((i,j),(punto2,punto2,punto2))
px1 = imagenNuevaX.load()
px2 = imagenNuevaY.load()
#Mezclar las mascaras
for i in range(x):
for j in range(y):
p1 = px1[i,j]
p2 = px2[i,j]
r = ( p1[0] + p2[0] ) / 2
g = ( p1[1] + p2[1] ) / 2
b = ( p1[2] + p2[2] ) / 2
imn.putpixel((i,j),(r,g,b))
imagenNuevaX.show()
imagenNuevaY.show()
return imn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment