document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import pygame
  2. from pygame.locals import *
  3. import Image
  4. import math
  5.  
  6. pygame.init()
  7. pantalla = pygame.display.set_mode((190,250))
  8.  
  9. # FOTO NORMAL
  10. imagen = pygame.image.load("imagen.jpg")
  11.  
  12.  
  13. #FOTO ESCALA DE GRISES
  14. image = Image.open("rayas.jpg")
  15. pixeles = image.load()
  16. ancho, altura =image.size
  17.  
  18. for x in range(ancho):
  19.  for y in range(altura):
  20.   (r,g,b) = image.getpixel((x,y))
  21.  #  promedio=((r+g+b)/3)
  22.    
  23.   pixeles[x,y] = (255,255,0)
  24. image.save("1.jpg")
  25. image.show()  
  26.  
  27.  
  28.  
  29. #Filtros
  30. image = Image.open("1.jpg")
  31. pixeles = image.load()
  32. ancho, altura =image.size
  33. numero=raw_input("Teclea el numero de filtro que quieres ")
  34. numerofiltro=int(numero)
  35.  
  36. for z in range(numerofiltro):
  37.    for x in range(ancho):
  38.     for y in range(altura):
  39.       contador = 1
  40.       promedio = 0
  41.     # Toma el RGB del pixel
  42.       (r,g,b) = pixeles[ x, y ]
  43.       promedio += ( r + g + b ) / 3
  44.     # Vecino Norte
  45.       try :
  46.         if x - 1 < 0:
  47.          None
  48.         else:
  49.          r1, g1, b1 = pixeles[ ( x - 1 ), y ]
  50.          promedio += ( r1 + g1 + b1 ) / 3
  51.          contador = contador + 1
  52.       except:
  53.         pass
  54.       # Vecino Sur
  55.       try :
  56.        if x + 1 >= ancho:
  57.          None
  58.        else:
  59.          r2, g2, b2 = pixeles[ ( x + 1 ), y ]
  60.          promedio += ( r2 + g2 + b2 ) / 3
  61.          contador = contador + 1
  62.       except:
  63.        pass      
  64.    # Vecino Oeste
  65.       try
  66.        if y - 1 < 0:
  67.         None
  68.        else:
  69.         r3, g3, b3 = pixeles[ x, ( y - 1 ) ]
  70.         promedio += ( r3 + g3 + b3 ) / 3
  71.         contador = contador + 1
  72.       except:
  73.        pass
  74. # Vecino Este
  75.       try:
  76.        if y + 1 >= altura:
  77.         None
  78.        else:
  79.         r4, g4, b4 = pixeles[ x, ( y + 1 ) ]
  80.         promedio += ( r4 + g4 + b4 ) / 3
  81.         contador = contador + 1
  82.       except:
  83.        pass
  84.       promedio /= contador  
  85. # Coloca el valor obtenido en el pixel actual
  86.       pixeles[ x, y ] = ( promedio, promedio, promedio )
  87. image.save(\'borrosa.png\', \'png\')  
  88. # image.show()
  89.  
  90.  
  91.  
  92.  
  93. #CONVOLUCION DESPUES DE  ESCALA DE GRISES Y FILTRADA
  94. image = Image.open("borrosa.jpg")
  95. pixeles = image.load()
  96. ancho, altura =image.size
  97. msobelX = ([-1, 0, 1], [-2, 0, 2], [-1, 0, 1])  #Para gradiente de  x.
  98. msobelY = ([1, 2, 1], [0, 0, 0], [-1, -2, -1])  #Para gradiente de y.
  99.  
  100. prewittX=([-1, 0, 1], [-1, 0, 1], [-1, 0, 1])#EJE X PREWITT
  101. prewittY=([1, 1, 1], [0, 0, 0], [-1,-1,-1])#EJE Y PREWITT
  102. tamanomatriz=3
  103. sumatoriaX = 0
  104. sumariaY = 0
  105. seleccion=raw_input("INGRESA 1 PARA SOBEL y DOS PARA PREWITT  ")
  106. matrizagarrada=int(seleccion)
  107.  
  108. if matrizagarrada==1:
  109.  for x in range(altura):
  110.   for y in range(ancho):
  111.    sumatoriaX = 0
  112.    sumatoriaY = 0
  113.    if x != 0 and y != 0 and y != ancho and x != altura:
  114.     for i in range(tamanomatriz):
  115.      for j in range(tamanomatriz):
  116.       try:
  117.        gx = msobelX[i][j]*pixeles[y+j, x+i][1]
  118.        gy = msobelY[i][j]*pixeles[y+j, x+i][1]
  119.    
  120.       except:
  121.        productosGX = 0
  122.        productosGY = 0
  123.      
  124.       sumatoriaX = gx+sumatoriaX
  125.       sumatoriaY = gy+sumatoriaY
  126.    
  127.     gxalcuadrado = pow(sumatoriaX, 2)
  128.     gyalcuadrado = pow(sumatoriaY, 2)
  129.     gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado))
  130.     pixelNuevo=gradienteResultante
  131.     if  pixelNuevo> 255:
  132.       pixelNuevo = 255
  133.  
  134.     if  pixelNuevo < 0:
  135.      pixelNuevo = 0
  136.    
  137.     pixeles[y,x] = ( pixelNuevo, pixelNuevo, pixelNuevo)  
  138. image.save(\'rayitassc.png\', \'png\')  
  139. #image.show()
  140.  
  141. if matrizagarrada==2:
  142.  for x in range(altura):
  143.   for y in range(ancho):
  144.    sumatoriaX = 0
  145.    sumatoriaY = 0
  146.    if x != 0 and y != 0 and y != ancho and x != altura:
  147.     for i in range(tamanomatriz):
  148.      for j in range(tamanomatriz):
  149.       try:
  150.        gx = prewittX[i][j]*pixeles[y+j, x+i][1]
  151.        gy = prewittY[i][j]*pixeles[y+j, x+i][1]
  152.    
  153.       except:
  154.        productosGX = 0
  155.        productosGY = 0
  156.      
  157.       sumatoriaX = gx+sumatoriaX
  158.       sumatoriaY = gy+sumatoriaY
  159.    
  160.     gxalcuadrado = pow(sumatoriaX, 2)
  161.     gyalcuadrado = pow(sumatoriaY, 2)
  162.     gradienteResultante = int(math.sqrt(gxalcuadrado+gyalcuadrado))
  163.     pixelNuevo=gradienteResultante
  164.     if  pixelNuevo> 255:
  165.       pixelNuevo = 255
  166.  
  167.     if  pixelNuevo < 0:
  168.      pixelNuevo = 0
  169.    
  170.     pixeles[y,x] = ( pixelNuevo, pixelNuevo, pixelNuevo)
  171. image.save(\'rayitas.png\', \'png\')  
  172. # image.show()  
  173.  
  174.  
  175.        
  176. while True:
  177.     for eventos in pygame.event.get():
  178.         if eventos.type == pygame.QUIT:
  179.             exit()
  180.     pantalla.blit(imagen,(0,0))
  181.     pygame.display.update()
');