Implementation of lossy image compression using the DCT method (JPEG standard), based on Ken Cabeen and Peter Gent document.
- Introduction
- Requirements
- Installation
- Usage
- How It Works
- DCT Equation
- Output Files
- Code Review Notes
- References
As our use of and reliance on computers continues to grow, so does the need for efficient ways of storing large amounts of data. This project implements JPEG-style lossy image compression using the Discrete Cosine Transform (DCT).
Key Concepts:
- Two kinds of compression algorithms: lossless and lossy image compression
- JPEG uses lossy compression with DCT to separate images into parts of different frequencies
- Through quantization, less important frequency information is discarded
- The reconstructed image contains some distortion, but file size is significantly reduced
- Python 3.x
- NumPy
- OpenCV (cv2)
- Matplotlib
# Install required packages
pip install numpy opencv-python matplotlib
# Or using requirements.txt (recommended)
pip install -r requirements.txtNote: Create a requirements.txt file with:
numpy>=1.19.0
opencv-python>=4.5.0
matplotlib>=3.3.0
python DCT.py <input_file> <compression_level># Medium compression (quality 50)
python DCT.py Image/car1.jpg 50
# High quality (low compression)
python DCT.py Image/car2.jpg 90
# Low quality (high compression)
python DCT.py Image/cameraman.tif 10Compression Level Guide:
1-30: High compression, lower quality30-70: Moderate compression, good balance70-100: Low compression, high quality (closer to original)
Important: The input image dimensions should be divisible by 8 for proper block processing. The script does not currently handle padding for non-conforming dimensions.
- Block Division: The image is broken into 8×8 blocks of pixels
- DCT Application: Working from left to right, top to bottom, the DCT is applied to each block
- Quantization: Each block is compressed through quantization (discarding high-frequency components)
- Storage: The array of compressed blocks is stored with reduced size
- Decompression: When desired, the image is reconstructed using Inverse DCT (IDCT)
- Convert image to RGB channels and subtract 128 (center around zero)
- Apply DCT to each 8×8 block using the DCT coefficient matrix
- Quantize DCT coefficients using JPEG standard quantization tables
- For decompression: multiply by quantization matrix and apply IDCT
- Add 128 back and merge channels to reconstruct the image
The Discrete Cosine Transform coefficient matrix is computed as:
Where the DCT coefficient matrix T is defined as:
- T[0,j] = 1/√8 for all j
- T[i,j] = √(2/8) × cos((2j+1)iπ/16) for i > 0
The script generates three output files in the current directory:
DCT.jpg- Visualization of DCT coefficientsAfter_Quantiz.jpg- Quantized DCT coefficientsDecompressed.jpg- Final reconstructed image
The script outputs:
- Compression/Decompression Time - Processing time for each operation
- RMS Error - Root Mean Square error between original and decompressed image
- SNR - Signal-to-Noise Ratio (higher is better)
-
DCT_compress.py is incomplete:
- Missing
quantization_level()function definition - Import statements at the bottom (should be at top)
- Contains Jupyter notebook artifacts
- Missing
-
Limited error handling:
- No validation for file existence
- No check if image dimensions are divisible by 8
- No exception handling for file I/O operations
-
Hardcoded outputs:
- Output files are always written to the current directory
- Output filenames cannot be customized
- Add input validation (file existence, dimensions)
- Add error handling with try-except blocks
- Make output paths configurable via command-line arguments
- Add function docstrings
- Fix DCT_compress.py or remove if not needed
- Add padding for images not divisible by 8
For more details about JPEG compression and DCT:
- See
Document/dct.pdfin this repository - DCT Equation Image
- Ken Cabeen and Peter Gent's original documentation
See repository for license information.
