Hacking Image Interpolation for Fun and Profit
Raster images normally look distorted or pixelated when enlarged — especially if they contain graphics or text — but the technique I’m about to show you is all about blowing up tiny images and the results might just surprise you.
Let’s say you want to use this image as a full page background on your website:
Using SVG or CSS to render the image 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 small 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
32x18
image as a CSS background image 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? As you can see, the stretched image looks almost identical to the original. In fact, it scales up infinitely without any loss in fidelity. Better yet,
the new image is only 1.4KB
— that’s a 93.6% reduction 🎉. Click here for a side by side comparison.
Here is another example:
Notice how this image has a bit more detail than the previous one. In this case, the small image will need 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 combined with 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 image data. Smooth images with less contrast can be upsampled without any distortion or pixelation because the interpolated pixels blend in with the original ones.
How is this useful?
This technique is great for reducing the file size of background images which often contain less detail. It’s also great for rendering lightweight, scalable mesh gradients — something you can’t do with SVG or CSS. Mesh gradients were originally 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
- Why Use a JavaScript Framework
13 Jul 2019 - Rendering Children in React
06 Feb 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
06 Feb 2013