A project for compresing images using SVD algorithm. The script splits the image into its red, green, and blue color channels, applies SVD to each channel, and then reconstructs the image with reduced dimensions. This compression technique can significantly reduce the size of the image while preserving its visual quality.
This project uses Numpy and PIL libraries.
The compress_color_image function performs the following steps:
image = Image.open(image_path)
A = np.array(image)R = A[:,:,0]
G = A[:,:,1]
B = A[:,:,2]def compress_channel(channel):
U, sigma, VT = np.linalg.svd(channel, full_matrices=False)
U_k = U[:, :k]
sigma_k = np.diag(sigma[:k])
VT_k = VT[:k, :]
return np.dot(U_k, np.dot(sigma_k, VT_k))
R_k = compress_channel(R)
G_k = compress_channel(G)
B_k = compress_channel(B)compressed_image_array = np.stack([R_k, G_k, B_k], axis=2)
compressed_image_array = np.clip(compressed_image_array, 0, 255)
compressed_image = Image.fromarray(compressed_image_array.astype('uint8'))
compressed_image.save(output_path)- Save the code file and move it to the image folder
- Rename the last code line
- Run the code