This repository contains code for JPEG image compression implemented in Python using the OpenCV library. The code includes encoding and decoding processes to compress and decompress an image, respectively.
- Python 3.x
- OpenCV
- NumPy
- Matplotlib
- Ensure that you have the required dependencies installed.
- Place the image file (
city.jpg
) in the same directory as the notebook. - Run the notebook to perform the encoding and decoding processes.
- The encoded data will be saved as
output_jpeg_encoded.npy
. - The decoded image will be saved as
compressed_image.jpg
. - The original and compressed images will be displayed side by side, along with the RMSE, PSNR, and compression ratio.
The encoding process quantizes the image using a predefined quantization matrix and performs discrete cosine transform (DCT) on the quantized coefficients. These coefficients are then serialized and saved as output_jpeg_encoded.npy
.
The decoding process reads the serialized data from output_jpeg_encoded.npy
and performs inverse DCT and dequantization to obtain the decompressed image, which is saved as compressed_image.jpg
. The RMSE, PSNR, and compression ratio between the original and compressed images are also calculated and displayed.
- The code assumes that the input image is in the RGB color space. If the image is in a different color space, modification to the code may be required.
- The code uses predefined quantization matrices for luminance (Y) and chrominance (U and V) channels. These matrices can be customized according to specific requirements.
- The code saves the compressed image as JPEG format (
compressed_image.jpg
). However, the image quality may vary depending on the desired compression ratio and the input image characteristics.