Hacking Image Interpolation for Fun and Profit
Raster images normally look distorted or pixelated when enlarged—especially if they contain graphics or text. The technique below is about blowing up tiny images, and the results might surprise you.
Let’s say you want to use this image as a full page background on your website:
Using SVG or CSS gradients would be ideal for reducing the file size and providing resolution-independent scaling. Sadly, neither technology supports mesh gradients at this time.
Fortunately, there’s another way to achieve both a tiny file size and vector-like scaling using a raster image.
Here’s how it works:
- Downsample the image so its dimensions are
32x18. - Export the image as a PNG. The resulting file
should be around 1 KB. - Use the
32x18image as a CSS background that covers its container:.my-background { width: 1920px; height: 1080px; background: url(32x18.png); background-size: cover; }or as an
<img>element:<img src="32x18.png" style="width: 100%; height: auto;" />
Here is the result:
Can you tell the difference? The stretched image looks almost identical to the original. It scales up infinitely without noticeable distortion. Better yet, the new image is only 1.4KB—a 93.6% reduction 🎉. Click here for a side-by-side comparison.
Here is another example:
This image has more detail than the previous one. In this case, the small image needs to be a bit larger (128x72) to capture the detail. Finding the right size takes some experimenting.
This technique is also suitable for blurred background photos—the kind you typically see behind a text overlay. In this case, saving the image as a JPEG yields a smaller file size.
Limitations
Before you get too excited, this technique does have one limitation. It works well for smooth gradients and images with less contrast, but look what happens if you blow up this image:
1920x1080-3.jpg (337 KB)
128x72-2.png (21 KB)
What’s going on here?
When the downsampled image is enlarged, the browser uses an interpolation algorithm to fill in the missing data. Smooth images with less contrast can be upsampled without noticeable distortion because the interpolated pixels blend in with the originals.
How is this useful?
This technique is great for reducing the file size of background images that often contain less detail. It’s also great for rendering lightweight, scalable mesh gradients—something you can’t currently do with SVG or CSS. Mesh gradients were planned for SVG 2.0 but the feature has since been removed from the spec.
So the next time you’re about to export a big background image from Photoshop or Sketch, give this technique a try. You might be surprised how well it works.
Discuss on Twitter • Edit on GitHub
Keep Reading
- Structured output was my LLM aha moment
11 Jun 2024 - Why Use a JavaScript Framework
13 Jul 2019 - Hacking Image Interpolation for Fun and Profit
13 Jan 2019 - The Perils of Jest Snapshot Testing
07 Jan 2019 - A Better Way to Test Your React-Redux App
01 Jan 2019 - Firebase + Create React App
01 Aug 2018 - Partial Application in Action
29 Sep 2017 - Using SVG to Shrink Your PNGs
07 Sep 2014 - Adaptive content everywhere
06 Feb 2013