Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import pywt, numpy
import Image
from sys import argv
import time
def crear_imagen(cef,esg): #Pasa los coeficientes a un filtro
for cs,i in enumerate(cef):
binariza(i,cs)
# raw_input()
def cef(array): #aplicar tranformada discreta de wavelets
data = array
#print 'data', data
coeffs = pywt.dwt2(data, 'Haar')
csA, (csH, csV, csD) = coeffs
print 'Coefic 1'
print csA
print 'Coefic 2'
print csH
print 'Coefic 3'
print csV
print 'Coefic 4'
print csD
return (csA,csH,csV,csD),coeffs
def escala_g(image): #convierte la imagen a escala de grises
imagen = image.load()
ancho,alto = image.size
for i in range(ancho):
for j in range(alto):
(r,g,b) = image.getpixel((i,j))
esg = (r+g+b)/3
imagen[i,j] = (esg,esg,esg)
df = image.save('escala.png')
return image
def toma_array(esg): # toma matriz de coeficientes
ancho,alto=esg.size
imagen=esg.load()
m = numpy.empty((ancho, alto))
for i in range(ancho):
for j in range(alto):
(r,g,b) = esg.getpixel((i,j))
m[i,j]=r
return m
def conversion_array(image):
matriz = numpy.fromstring(image.tostring(), numpy.uint8)
matriz.shape = (image.size[1], image.size[0], len(image.getbands()))
return matriz
def binariza(array,cs): #filtro para obtener valores menores
print array
ancho=len(array)
alto=len(array[0])
val=0
im = Image.new('RGB', (ancho, alto), (255, 255, 255))
im.save('nuevab.png')
imagen=im.load()
for i in range(ancho):
for j in range(alto):
if array[i,j]<0:
val = 0
elif array[i,j]>255:
val= 255
else:
val=int(array[i,j])
imagen[i,j]=(val,val,val)
im.save('coef'+str(cs)+'.png')
def descomprimir(cs,esg): #imagen final comprimida
d=pywt.idwt2(cs, 'haar')
imagen=esg.load()
ancho,alto=esg.size
for i in range(ancho):
for j in range(alto):
imagen[i,j]=(int(d[i,j]),int(d[i,j]),int(d[i,j]))
esg.save('descomprimida.png')
def main(): #aplica funciones a la imagen
size=(400,400)
img=Image.open(argv[1])
esg=escala_g(img)
esg.thumbnail(size, Image.ANTIALIAS)
m=toma_array(esg)
cs,cof=cef(m)
crear_imagen(cs,esg)
descomprimir(cof,esg)
cs,cof=cef(cs[0])
crear_imagen(cs,esg)
cs,cof=cef(cs[0])
crear_imagen(cs,esg)
esg.save('Image.png')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment