Aquí está el codigo:
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 | |
import Image | |
import numpy | |
def blancoynegro(img): | |
ancho, alto = img.size | |
img = img.copy() | |
pixel = img.load() | |
for i in range(ancho): | |
for j in range(alto): | |
(r, g, b) = pixel[i,j] | |
bn = int((r + g + b) / 3) | |
pixel [i, j] = (bn, bn, bn) | |
return img | |
def matpix(img): | |
ancho, alto = img.size | |
img = img.copy() | |
pixel = img.load() | |
matriz = numpy.empty((ancho, alto)) | |
for i in range(ancho): | |
for j in range(alto): | |
(r,g,b) = img.getpixel((i,j)) | |
matriz[i,j] = r | |
return matriz | |
def Coeffs(matriz): | |
coeffs = pywt.dwt2(matriz, 'haar') | |
cA,(cH,cV,cD) = coeffs | |
return (cA,cH,cV,cD), coeffs | |
def comp(coeficientes,bw): | |
wav =pywt.idwt2(coeficientes,'haar') | |
pixel = bw.load() | |
ancho,alto= bw.size | |
for i in range(ancho): | |
for j in range(alto): | |
newp = int(wav[i,j]) | |
pixel[i,j]=(newp,newp,newp) | |
return bw | |
def main(): | |
img = image.copy() | |
img.thumbnail((128,128), Image.ANTIALIAS) | |
bw = blancoynegro(img) | |
matrix = matpix(img) | |
coefs, co = Coeffs(matrix) | |
img = comp(co, bw) | |
img.save("compressed_jake.jpg") | |
if __name__ == '__main__': | |
image = Image.open("jake.jpg").convert('RGB') | |
main() |
.....
y el resultado:
(original)
(comprimida)
Y las diferencias en los tamaños:
Referencias
Basic JPEG: http://www.whydomath.org/node/wavlets/basicjpg.html
El reporte está bien mínimo y te da 4 puntos; el código no tiene comentarios ni otra explicación y no es en realidad un algoritmo original. Van otros 4 por el código.
ResponderEliminar