Image Processing 8. Image scaling
2 min read June 23, 2025 #image processing #javascriptImage Processing 1. Introduction
Image Processing 2. Simple transformation
Image Processing 3. Negate filter, channel extraction and inversion
Image Processing 4. Desaturation
Image Processing 5. Color Models
Image Processing 6. Brightness, saturation, contrast, gamma correction
Image Processing 7. Histogram
Image Processing 8. Image scaling
Image Processing 9. Blur
Let's explore a highly relevant topic in image processing: image scaling. You've likely encountered this task and may have even heard about interpolation algorithms used for scaling. This article will focus on that.
Nearest-neighbor Interpolation#
When scaling using this method, we only manipulate pixels without needing to obtain the values of individual color channels. This allows images to scale very quickly. However, when downscaling - many details are lost, and when upscaling - pixels turn into large squares or rectangles.
;
;
for ; y < newHeight; y++
The original image of size width × height
is scaled to newWidth × newHeight
. We calculate the increments dx
and dy
, then for each new pixel, we find the corresponding position in the original image and assign the value.
Original (64×64) in the center, downscaled copy (32×32) on the left, and upscaled version (200×200) on the right:
Bilinear Interpolation#
For each point on the image, values of four neighboring pixels are sampled. The value for each color component is calculated using a formula and assigned in the current position.
;
;
;
;
;
for ; i < newHeight; i++
Similarly, we calculate the increment and the distance between old and new positions (xDiff
, yDiff
), then apply the interpolate
function for each color component.
Lanczos Interpolation#
Finally, let's discuss interpolation using the Lanczos filter. This method provides even smoother results, but significantly increases processing time.
The filter can be applied with different kernel sizes (convolution kernels will be covered in a separate article). While bilinear interpolation uses 4 source pixels per new pixel, Lanczos may use 9 (3×3), 25 (5×5), 49 (7×7), etc. Sizes 2 and 3 offer the best balance of quality and speed.
Result for filterSize=2
:
Finally, a Pikachu downscaled to 20×20 and then upscaled: