Overview
Most perceptual hashing algorithms resize input images to a fixed dimension before computing the hash. The interpolation method used during this resize operation can significantly impact hash quality and computation speed. imghash provides 7 interpolation algorithms, each with different trade-offs between quality and performance.Available Methods
NearestNeighbor
Selects the pixel value from the nearest source pixel without any blending.Speed
Fastest - No computation, direct pixel lookup
Quality
Lowest - Can produce blocky, aliased results
Example
Characteristics
- No anti-aliasing
- Sharp edges preserved (can be jagged)
- Best for: pixel art, binary images, maximum speed
- Avoid for: natural images with gradients
Bilinear
Default for most algorithms. Blends the 4 nearest pixels using linear interpolation.Speed
Fast - Simple 4-pixel weighted average
Quality
Good - Smooth results for most use cases
Example
Algorithm
Characteristics
- Smooth gradients
- Minimal blur
- Good balance of speed and quality
- Recommended for: Average, Difference, Median, PHash, most algorithms
BilinearExact
More precise bilinear interpolation using exact floating-point math.Speed
Slightly slower than standard Bilinear
Quality
More accurate floating-point calculations
Example
Use When
- You need precise color values (ColorMoment, CLD)
- Reproducibility across platforms is critical
- Computational cost is not a concern
Bicubic
Uses 16 nearest pixels with cubic polynomial weighting for smoother results.Speed
Slower - 16 pixel computations per output pixel
Quality
Higher - Smoother than bilinear, less aliasing
Example
Algorithm
Characteristics
- Smoother gradients than bilinear
- Can introduce slight ringing near edges
- Better preservation of high-frequency details
- Recommended for: GIST, Zernike, high-quality requirements
Bicubic can produce slightly sharper results than bilinear, which may improve discriminability for some algorithms.
MitchellNetravali
Advanced bicubic filter with balanced sharpness and smoothness.Speed
Similar to Bicubic - 16 pixel kernel
Quality
Excellent - Optimized trade-off of blur and ringing
Example
Characteristics
- Uses Mitchell-Netravali filter with B=1/3, C=1/3
- Perceptually balanced - minimal blur and ringing
- Preferred by many image processing experts
- Recommended for: Production systems requiring high quality
Lanczos2
Sinc-based filter with 2-lobe kernel for high-quality resampling.Speed
Slow - 4x4 kernel with sinc calculations
Quality
Very High - Excellent detail preservation
Example
Algorithm
Characteristics
- Based on sinc function (ideal low-pass filter)
- Sharp transitions, minimal blur
- Can introduce ringing artifacts near edges
- Recommended for: Downsampling photographs, detail-critical applications
Lanczos3
Sinc-based filter with 3-lobe kernel - highest quality, slowest speed.Speed
Slowest - 6x6 kernel with sinc calculations
Quality
Highest - Maximum detail retention
Example
Algorithm
Characteristics
- Widest kernel - most source pixels considered
- Sharpest results, best frequency preservation
- Most prone to ringing artifacts
- Recommended for: Critical applications, large downsample ratios, research
Setting Interpolation
All algorithms that perform resizing accept theWithInterpolation option:
Algorithms Using Interpolation
These algorithms resize images and supportWithInterpolation:
- Average, Difference, Median, PHash
- BlockMean, MarrHildreth
- ColorMoment, CLD, EHD
- LBP, HOGHash
- RadialVariance, Zernike
- GIST, WHash
Some algorithms (PDQ, BoVW, RASH) use fixed internal resizing and don’t expose interpolation as an option.
Performance Comparison
| Method | Relative Speed | Quality | Kernel Size | Use Case |
|---|---|---|---|---|
| NearestNeighbor | 1.0x (baseline) | ⭐ | 1x1 | Speed-critical, pixel art |
| Bilinear | 0.8x | ⭐⭐⭐ | 2x2 | Default - balanced |
| BilinearExact | 0.75x | ⭐⭐⭐ | 2x2 | Precise color values |
| Bicubic | 0.4x | ⭐⭐⭐⭐ | 4x4 | Natural images |
| MitchellNetravali | 0.4x | ⭐⭐⭐⭐ | 4x4 | Production quality |
| Lanczos2 | 0.3x | ⭐⭐⭐⭐⭐ | 4x4 | Photographs |
| Lanczos3 | 0.2x | ⭐⭐⭐⭐⭐ | 6x6 | Maximum quality |
Speed measurements are approximate and depend on image size and CPU architecture.
Quality vs Speed Trade-offs
- Speed Priority
- Balanced
- Quality Priority
- Processing millions of images
- Real-time video hashing
- Mobile/embedded devices
Interpolation Impact on Hashes
Different interpolation methods produce slightly different hashes from the same image:Best Practices
Use Bilinear as Default
Unless you have specific requirements, Bilinear provides excellent quality with minimal overhead.
Match Interpolation to Algorithm
Simple algorithms (Average, Difference) don’t benefit much from high-quality interpolation. Save Lanczos for complex algorithms like GIST or Zernike.
Consider Downsample Ratio
Larger downsample ratios (e.g., 4K → 8x8) benefit more from high-quality interpolation than small ratios.
Profile Your Application
Measure actual performance impact. Interpolation cost is often small compared to algorithm computation.
Recommendations by Algorithm
Simple Bit-based (Average, Difference, Median)
Simple Bit-based (Average, Difference, Median)
Recommended: Bilinear (default)These algorithms threshold pixels to binary values, so subtle interpolation differences matter less.
DCT-based (PHash, PDQ)
DCT-based (PHash, PDQ)
Recommended: Bicubic or MitchellNetravaliDCT benefits from smooth frequency domain representation.
Color/Histogram (ColorMoment, CLD, EHD)
Color/Histogram (ColorMoment, CLD, EHD)
Recommended: BilinearExact or BicubicColor precision matters for these algorithms.
Gradient-based (HOGHash, LBP)
Gradient-based (HOGHash, LBP)
Recommended: Bicubic or Lanczos2Edge preservation is important for gradient calculation.
Advanced Features (GIST, Zernike, RadialVariance)
Advanced Features (GIST, Zernike, RadialVariance)
Recommended: Lanczos2 or Lanczos3These algorithms analyze fine details and benefit from high-quality resampling.
Error Handling
String Representation
Advanced: Custom Interpolation
For custom resize logic, you can pre-process images before hashing:This approach gives full control but requires more code and understanding of the algorithm’s expected input size.
Related
- Hash Types - Understanding hash representations
- Choosing an Algorithm - Algorithm selection and configuration