File: //test_python/ricostruttore_light.py
import numpy as np
from PIL import Image
def ricostruttore_light():
data = np.load('sasso_v4_light.npz')
shape = data['shape']
reconstructed_img = np.zeros(shape)
for color in ['r', 'g', 'b']:
# Riportiamo in float32 per il calcolo matriciale onde evitare overflow
U = data[f'U_{color}'].astype(np.float32)
S = data[f'S_{color}'].astype(np.float32)
Vt = data[f'Vt_{color}'].astype(np.float32)
channel_idx = {'r':0, 'g':1, 'b':2}[color]
reconstructed_img[:,:,channel_idx] = np.dot(U, np.dot(np.diag(S), Vt))
# Riconversione finale
reconstructed_img = np.clip(reconstructed_img * 255, 0, 255).astype(np.uint8)
Image.fromarray(reconstructed_img).save("VOLTO_OTTIMIZZATO.png")
print("Ricostruzione completata: controlla VOLTO_OTTIMIZZATO.png")
if __name__ == "__main__":
ricostruttore_light()