Skip to content
docs
GitHub 4.5M /week v2.2.0
Loading & saving

blurhash()

Encode the image as a short BlurHash string for blurred placeholders.

From @jimp/plugin-hash

blurhash encodes the image as a BlurHash: a string of about 28 characters that describes the image’s colors and their rough layout. Store it next to the image, and show it with decodeBlurhash or any BlurHash library as a blurred placeholder while the real image loads.

Signature

blurhash.d.ts
image.blurhash(options?: { componentsX?: number; componentsY?: number }): string

blurhash doesn’t change the image. It returns the hash as a string.

Options

Option Type Default Description
componentsX number 4 How many components across, a whole number from 1 to 9. More keeps more horizontal detail.
componentsY number 3 How many components down, a whole number from 1 to 9. More keeps more vertical detail.

The hash is 4 + 2 * componentsX * componentsY characters long: 28 with the defaults.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("dice.png");
image.background = 0xffffffff; // draw transparent pixels over white
image.blurhash(); // "LqQ0K:jL_NtMD+V{t6k7yDahMdj="
image.blurhash({ componentsX: 3, componentsY: 3 }); // "KqQ0K:jL_ND+V{t6yDahMd"

Store a placeholder with each image

Hash each image once, when it’s uploaded or built, and send the hash with the image’s URL and size:

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
const entry = {
src: "/images/photo.jpg",
width: image.width,
height: image.height,
blurhash: image.blurhash(),
};

The page decodes the hash at a small size and stretches it over the image’s box until the image loads. decodeBlurhash does this with Jimp, and the blurhash package does it in the browser. The hashes are the same as that package’s, so either side can be swapped.

To serve hashes from @jimp/server, request an image with fm=blurhash.

Try it

Change the components to see how much detail the placeholder keeps.

Try blurhash()
const hash = image.blurhash()

Encode the image as a BlurHash string, then decode it at the image's size to see the placeholder it makes.

4
3
1
#ffffff
Loading Jimp…Drop an image to load it
PNG
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");

Notes

  • Speed. Images larger than 64 pixels on their longest side are scaled down before they’re encoded, so large photos are quick to hash. The components only hold coarse detail, which survives the scaling.
  • Transparency. BlurHash has no alpha. Transparent pixels are drawn over image.background, the way getBuffer draws them for JPEG. The background is transparent by default, which turns fully transparent pixels black, so set image.background to the color of the page.
  • Color spaces. BlurHash works in sRGB. Images in other color spaces, such as Display P3, are converted to sRGB first.
  • Aspect ratio. The hash doesn’t store the image’s size. For tall or wide images, more components along the long side keep more detail, such as { componentsX: 3, componentsY: 4 } for a portrait photo.
  • Invalid options. Components that aren’t whole numbers from 1 to 9 throw a validation error.
  • Added in 2.2.