How to Compress Images with SVD and TensorFlow Core APIs

This article explains how to compress images using singular value decomposition (SVD) and TensorFlow Core APIs. Learn to apply this technique to obtain low-rank approximations and reduce the size of your images efficiently.

martes, 12 de agosto de 2025 • 4 min read • Q2BSTUDIO Team

Artificial-Intelligence-

How to compress images with SVD and TensorFlow Core APIs: this article explains in a practical and accessible way how to use singular value decomposition (SVD) to obtain low-rank approximations and apply image compression using TensorFlow Core APIs.

Key concepts: singular value decomposition factorizes a matrix A as A = U Sigma V^T where Sigma contains non-negative singular values ordered from largest to smallest. A rank-k approximation is built by taking the first k columns of U and V and the first k singular values in Sigma, obtaining A_k = U_k Sigma_k V_k^T. The best approximations in norm 2 or Frobenius norm are achieved with this truncation.

General step to compress an image: load the image and convert it to a numeric matrix, normalize to floating point, compute the SVD, truncate to a chosen rank k, reconstruct the image from the approximation, and quantize to save. For color images, SVD can be applied per RGB channel or work in YUV space and mainly compress the luminance channel.

Example flow with TensorFlow Core APIs and Python: import libraries, read and normalize the image, call tf.linalg.svd, select k singular values, and reconstruct. Illustrative example code in Python without high-level dependencies:

import tensorflow as tf import numpy as np from PIL import Image img = Image.open(path_to_image).convert(L) arr = np.array(img).astype(np.float32) / 255.0 matrix = tf.constant(arr) s, u, v = tf.linalg.svd(matrix, full_matrices=False) k = 50 u_k = u[:, :k] s_k = tf.linalg.diag(s[:k]) v_k = v[:, :k] recon = tf.matmul(u_k, tf.matmul(s_k, v_k, transpose_b=True)) recon_np = (recon.numpy() * 255).astype(np.uint8) Image.fromarray(recon_np).save(path_to_output)

Practical notes on implementation: use full_matrices False for efficiency; choose k according to the quality-compression trade-off; for large images work with tiles or subsampling if memory is limited; for color, repeat the process per channel or use a color transformation before SVD.

Evaluation strategies: compression factor = original_size / compressed_size where compressed_size considers storing U_k S_k V_k^T in compact form; energy retention = sum_{i=1..k} s_i^2 / sum_{i=1..n} s_i^2 which indicates what fraction of total energy is preserved; quantitative error = average MSE and PSNR to measure perceptual degradation; error visualization = show the difference between original and reconstructed image and heat maps to locate losses.

How to choose k: define an energy retention threshold, for example 0.95, and select the smallest k that satisfies sum_{i=1..k} s_i^2 / sum_{i=1..n} s_i^2 >= 0.95. Alternatively, optimize for a target compression factor and validate with perceptual metrics and visual tests.

Advantages and limitations: SVD provides optimal linear approximations and allows direct control of compression through k. It is limited in fine textures and complex edges where detail loss can be visible. SVD compression is more computationally expensive than specialized methods and is often combined with quantization and entropy coding for efficient storage.

Optimization tips in TensorFlow: leverage optimized matrix operations, use GPU when possible, process channels in parallel, and convert to more compact data types for final storage. For large data volumes, consider randomized SVD approximations to speed up computation while maintaining good quality.

Use cases and applications: reduce the size of image datasets for machine learning, create previewable versions of images in web applications, save bandwidth in transmission and storage, and generate bases for restoration and super-resolution techniques.

Corporate integration and professional services: at Q2BSTUDIO we are a custom software and application development company specialized in artificial intelligence solutions applied to real problems. We offer custom software services, custom application development, cybersecurity, aws and azure cloud services, business intelligence services, ai for companies, AI agents, and power bi. We can design efficient compression and storage pipelines, integrate models that automate the selection of k parameters, and deploy secure and scalable cloud solutions.

How we can help: requirements audit, prototyping with TensorFlow Core APIs, optimization for production in aws and azure cloud environments, integration with business intelligence dashboards such as power bi, and deployment of AI agents to manage automated processes. Our team combines experience in artificial intelligence, cybersecurity, and custom software development to offer complete solutions.

Final summary: compressing images with SVD and TensorFlow Core APIs is a powerful technique when explicit control of precision versus compression is required. By applying evaluations such as energy retention and PSNR and combining SVD with practical implementation and deployment strategies, competitive results can be obtained for multiple scenarios. If you are looking for a professional custom solution or want to explore a prototype tailored to your use case, contact Q2BSTUDIO for advice and development.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.