A newer version of the Streamlit SDK is available: 1.62.0
Image Sampling and Quantization Demo - Project Summary
Overview
This interactive educational demo teaches fundamental concepts in digital image processing:
- Spatial Sampling: How resolution affects image quality and storage
- Quantization: How bit depth impacts color representation
- Compression: Differences between PNG (lossless) and JPEG (lossy)
Key Features
Interactive Controls
- Sampling Grid Size Slider (1-16x): Shows pixelation effect and pixel count reduction
- Bits per Pixel Slider (1-8 bits): Demonstrates color depth and posterization
- JPEG Quality Slider (1-100): Shows compression artifacts
Real-Time Feedback
- Live File Size Estimates: Shows how settings affect storage requirements
- Side-by-Side Comparisons: Original vs. processed images
- Detailed Calculations: Transparent math showing how sizes are computed
Educational Content
- Three Educational Tabs: Dedicated sections for Sampling, Quantization, and Compression
- Visual Examples: All concepts demonstrated with live image processing
- Formula Explanations: Mathematical basis for all calculations
Educational Objectives
Students will learn:
- The relationship between sampling rate and image resolution
- How quantization affects color depth and file size
- The trade-offs between image quality and storage
- Differences between lossy and lossless compression
- How JPEG blocking artifacts occur
Project Structure
sampling-quantization/
βββ app.py # Main Streamlit application (500+ lines)
βββ requirements.txt # Python dependencies
βββ README.md # Main documentation
βββ QUICKSTART.md # Getting started guide
βββ CONTRIBUTING.md # Contribution guidelines
βββ IMAGES.md # Guide for sample images
βββ LICENSE # MIT License
β
βββ Configuration Files:
βββ packages.txt # System dependencies for HF Spaces
βββ .python-version # Python 3.11
βββ .gitignore # Git ignore rules
βββ pyproject.toml # Project metadata
βββ Dockerfile # Docker deployment
βββ Makefile # Convenient make commands
β
βββ Scripts:
βββ setup.sh # Initial setup with venv
βββ run_simple.sh # Quick local run
βββ deploy.sh # Deploy to HuggingFace
βββ check_status.sh # Check deployment status
Quick Start
For Users (Simplest)
chmod +x run_simple.sh
./run_simple.sh
Using Make
make setup # First time only
make run # Start the app
Manual Setup
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
streamlit run app.py
Deployment Options
1. Hugging Face Spaces (Recommended)
./deploy.sh
- Free hosting
- Automatic builds
- Share with students via URL
- No server maintenance
2. Docker
docker build -t sampling-demo .
docker run -p 8501:8501 sampling-demo
3. Local Server
streamlit run app.py --server.port 8501
Teaching with This Demo
Suggested Lesson Plan
Part 1: Sampling (15 minutes)
- Start with original image at 1x sampling, 8 bits
- Gradually increase sampling rate (2x, 4x, 8x, 16x)
- Discuss: When does text become unreadable?
- Calculate storage savings
Part 2: Quantization (15 minutes)
- Reset sampling to 1x
- Reduce bits per pixel (8 to 4 to 2 to 1)
- Discuss: Color banding, posterization
- Show grayscale interpretation
Part 3: Compression (20 minutes)
- Apply moderate sampling/quantization
- Compare PNG vs JPEG at different qualities
- At JPEG quality < 30: Point out 8x8 blocking
- Discuss use cases for each format
Part 4: Interactive Exploration (10 minutes)
- Let students experiment with their own images
- Find optimal settings for different use cases
- Calculate real-world storage requirements
Discussion Questions
- What is the minimum acceptable sampling rate for your use case?
- How many bits per pixel do you really need for grayscale medical images?
- When would you choose PNG over JPEG and vice versa?
- Why do JPEG artifacts appear in 8x8 blocks?
- What's the total storage for 1000 photos at different settings?
Technical Details
Image Processing Pipeline
Original Image
β
Spatial Sampling (downsample to upsample with nearest neighbor)
β
Quantization (reduce bits per channel)
β
Compression (PNG lossless or JPEG lossy)
β
Display + File Size Calculation
File Size Calculation
Raw Size = (Width / Sampling) x (Height / Sampling) x Channels x Bits / 8
PNG Size β Raw Size x 0.7 (typical compression ratio)
JPEG Size β Raw Size x 0.3 (typical compression ratio)
Key Algorithms
- Downsampling:
cv.INTER_AREA(best quality for reduction) - Upsampling:
cv.INTER_NEAREST(shows pixelation clearly) - Quantization:
floor(value / step) * stepwherestep = 256 / 2^bits - JPEG: OpenCV's
cv.imencode()with quality parameter
Customization
Change Default Image
Edit load_sample_image() in app.py:
image_path = hf_hub_download(
repo_id="your-username/your-dataset",
filename="your-image.jpg",
repo_type="dataset",
)
Adjust Slider Ranges
In app.py, modify slider parameters:
sampling_rate = st.sidebar.slider(
"Sampling Grid Size (pixels)",
min_value=1, # Change these
max_value=32, # Change these
value=1,
)
Add New Features
See CONTRIBUTING.md for guidelines on adding:
- New compression algorithms
- Additional image metrics
- Interactive exercises
- Batch processing
Performance
- Load Time: < 2 seconds on first load (with caching)
- Interactive Response: Real-time (< 100ms per slider change)
- Memory Usage: ~200-300 MB (depends on image size)
- Supported Image Sizes: Up to 4K (auto-resized to 512px for demo)
Known Limitations
- Very large images (>10MB) may be slow - auto-resized to 512px
- JPEG artifact visibility depends on image content
- File size estimates are approximate (actual compression varies)
- Generated sample image is simple (encourage uploading real images)
Educational Resources
Concepts covered align with:
- Digital Image Processing (Gonzalez & Woods)
- Computer Vision fundamentals courses
- Signal processing curricula
- Compression theory courses
Contributing
We welcome contributions! See CONTRIBUTING.md for:
- Bug reports
- Feature requests
- Code contributions
- Documentation improvements
- Educational content enhancements
License
MIT License - Free for educational and commercial use
Acknowledgments
- Inspired by interactive teaching tools in computer vision
- Built with Streamlit for rapid prototyping
- OpenCV for image processing
- HuggingFace for free hosting
Support
- Issues: GitHub Issues for bugs and features
- Questions: Discussion board for educational questions
- Documentation: See README.md, QUICKSTART.md, IMAGES.md
Success Stories
Perfect for:
- Graduate image analysis courses
- Computer vision fundamentals
- Self-paced online learning
- Workshop demonstrations
- Research group tutorials
--- Ready to start? Run ./run_simple.sh and explore!