Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
832 B
2
Indexable
Never
import numpy as np
import pywt
from PIL import Image
from iht import iht, F2Coeffs

n_iter = 500
original_img = np.asarray(Image.open('./problem3/cassini128.png'))

params = [
    [1024, 0.0010],
    [2048, 0.0007],
    [4096, 0.0005]
]

def reconstruct(coef, img_name):
    Coeffs = F2Coeffs(coef)
    image = pywt.waverec2(Coeffs, 'db1')
    img = Image.fromarray(image.astype('uint8'))
    img.save(img_name)

# 3.2
for p in params:
    measurement, alpha = p[0], p[1]
    with open(f'./problem3/data/P_{measurement}.csv', 'r') as f:
        lines = f.readlines()
    lines = [float(line.rstrip()) for line in lines]

    R = np.genfromtxt(f'./problem3/data/R_{measurement}.csv', delimiter=',')
    P = F.reshape(-1, 1)

    F = iht(R, P, max(measurement/4, 1638), alpha=alpha)
    reconstruct(F, f'problem3_2_{measurement}.png')