Last active
December 17, 2015 20:58
-
-
Save AdriC1705/5670981 to your computer and use it in GitHub Desktop.
This file contains 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
import pywt | |
from PIL import Image | |
import numpy | |
import os | |
import Gnuplot | |
from time import * | |
def matriz(foto): | |
ancho,alto=foto.size | |
pixeles=foto.load() | |
matrix=numpy.empty((ancho,alto)) | |
for i in range(ancho): | |
for j in range(alto): | |
(r,g,b)=foto.getpixel((i,j)) | |
matrix[i,j]=r | |
return matrix | |
def coeficientes(matrix):#Obtener los coefficientes | |
coeffs=pywt.dwt2(matrix,'haar') | |
cA,(cH,cV,cD)=coeffs | |
return (cA,cH,cV,cD),coeffs | |
def binario(mat,c,to,ti): | |
ancho=len(mat) | |
alto=len(mat[0]) | |
nuevo=0 | |
foto=Image.new('RGB',(ancho,alto)) | |
pixeles=foto.load() | |
for i in range(ancho): | |
for j in range(alto): | |
if mat[i,j]<0: | |
nuevo=0 | |
elif mat[i,j]>255: | |
nuevo=255 | |
else: | |
nuevo=int(mat[i,j]) | |
pixeles[i,j]=(nuevo,nuevo,nuevo) | |
foto.save('coefficiente'+str(c)+'.jpg') | |
def inversa(coefficientes,escala): | |
inv=pywt.idwt2(coefficientes,'haar') | |
pixeles=escala.load() | |
ancho,alto=escala.size | |
for i in range(ancho): | |
for j in range(alto): | |
nuevo=int(inv[i,j]) | |
pixeles[i,j]=(nuevo,nuevo,nuevo) | |
return escala | |
def main(): | |
img=str(raw_input('Nombre de imagen: '))#cargar imagen | |
foto=Image.open(img) | |
ancho,alto=foto.size | |
f=os.stat(img) | |
print f | |
ti=time() | |
tamano_original=f.st_size | |
matrix=matriz(foto) | |
coe,coeff=coeficientes(matrix) | |
nueva_imagen(coe,tamano_original) | |
foto=inversa(coeff,foto) | |
foto.save('Inversa.jpg') | |
f=os.stat('Inversa.jpg') | |
tamano=f.st_size | |
porcentaje=(tamano*100)/float(tamano_original) | |
print "Resultado de imagen" | |
print "Tamano Imagen original:"+str(tamano_original)+"bytes" | |
print "Tamano con compression:"+str(tamano)+"bytes" | |
print "Porcentaje de compression:"+str(100-porcentaje)+"%" | |
print "Tiempo de compression: "+str(tt)+"segundos" | |
print "-------------------------------------------" | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment