- Overview
- Installation
- Quick Start
- Features
- Compression Techniques
- Interactive Demo
- API Reference
- Performance Metrics
- Contributing
- License
This library provides a comprehensive toolkit for image and video compression using various techniques, including DCT-based encoding, Huffman coding, and adaptive quantization. It's designed for educational purposes and practical applications, allowing users to understand compression techniques and apply them to their own projects.
# Clone the repository
git clone https://github.com/Buddhi19/
cd image-video-compression
# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtimport cv2
from Basic_Codec.codec import BasicCodec
# Load an image
img = cv2.imread('path/to/image.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (256, 256)) # Resize for demonstration
# Create a codec with specified parameters
codec = BasicCodec(img, macro_block_size=8, quantization_level=50)
# Compress and decompress the image
reconstructed_image = codec.IMAGE_CODEC()
# Evaluate the compression
print(f"PSNR: {PSNR(img, reconstructed_image)} dB")- Multiple Compression Techniques: DCT transformation, quantization, Huffman encoding
- Adaptive Bitrate Control: Target specific bitrates for your compressed media
- Quality Metrics: Evaluate compression with PSNR, MSE, and SSIM metrics
- Interactive Demo: Visual interface for experimenting with compression parameters
- Extensible Architecture: Easy to extend with new algorithms and techniques
DCT Transformation
The Discrete Cosine Transform (DCT) converts image data from the spatial domain to the frequency domain, where it can be more efficiently compressed.
def DCT(block):
"""
Apply 2D Discrete Cosine Transform to an image block
"""
return cv2.dct(np.float32(block))Quantization
Reduces the precision of the frequency components based on the quantization level.
def QUANTIZE(dct_block, quantization_level):
"""
Quantize DCT coefficients using the specified quantization level
"""
# Higher quantization level = more compression, lower quality
return np.round(dct_block / quantization_level) * quantization_levelHuffman Coding
Uses variable-length codes for symbols based on their frequency of occurrence.
def Huffman_Code(probabilities):
"""
Generate Huffman codes for given symbol probabilities
"""
# Implementation details in EntropyCoding_Library/Huffman.pyRun the interactive demo to experiment with different compression parameters:
python Basic_Codec/main.pyThe demo provides sliders for:
- Quantization level
- Macro block size
- Target bitrate
Watch in real-time as adjusting these parameters affects:
- Compressed file size
- Image quality (PSNR)
- Compression ratio
- Encoding/decoding time
class BasicCodec:
def __init__(self, img, macro_block_size, quantization_level):
"""
Initialize the codec with an image and compression parameters
Parameters:
-----------
img : numpy.ndarray
Input grayscale image
macro_block_size : int
Size of macro blocks (e.g., 8 for 8x8 blocks)
quantization_level : int
Quantization strength (higher = more compression)
"""
def encrypt_huffman_tree(self, file_path):
"""Save encrypted Huffman tree to file"""
def decrypt_huffman_tree(self, file_path):
"""Load and decrypt Huffman tree from file"""
def encrypt_image(self, file_path):
"""Compress and save image to file"""
def decrypt_image(self, file_path):
"""Load and decompress image from file"""
def IMAGE_CODEC(self):
"""Complete compression-decompression pipeline"""class BasicCodec_For_Given_Bitrate:
def __init__(self, img, macro_block_size, bit_rate):
"""
Initialize codec with target bitrate constraint
Parameters:
-----------
img : numpy.ndarray
Input grayscale image
macro_block_size : int
Size of macro blocks
bit_rate : int
Target bitrate in bits per second
"""
def find_optimal_quantization_level(self, low, high, fps):
"""Find optimal quantization level for target bitrate"""
def get_codec(self, quantization_level):
"""Create codec with specified quantization level"""
def evaluate_codec(self, fps):
"""Evaluate codec performance"""The library provides several metrics to evaluate compression performance:
-
PSNR (Peak Signal-to-Noise Ratio): Measures the quality of reconstructed image
PSNR(original, compressed) # Higher is better
-
Bit Rate: Measures the number of bits used per second
bit_rate(compressed_path, huffman_code_path, fps) # Lower is more compressed
-
Compression Ratio: Ratio of original file size to compressed file size
compression_ratio(original_path, compressed_path) # Higher is more compressed