This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.
Baseline date: 2026-10-07. This page follows the reference implementation in the
woltapp/blurhash repository: its
Algorithm.md and the
reference TypeScript code. The numbers in the worked
example were produced with the blurhash npm package (version 2.0.5) and cross-checked against a Python port. The cover examples further down were run with that package,
blurhash-java 1.0.0, blurhash-python 1.2.2, blurhash 1.1.5 (the other Python package) and thumbhash 0.1.1.
A BlurHash is a short string, usually 20 to 30 characters, that describes a blurry version of an image. The backend
stores it next to the image and returns it in the API response. The frontend decodes it to a few pixels in a few
milliseconds, scales it up and shows it while the real image downloads, see
BlurHash in the browser. Compared with a tiny JPEG or a solid
colour, it is smaller than any image file, needs no extra request, and looks like the image.
The string is the image’s lowest few frequencies. It is a coarse DCT (discrete cosine transform), the same idea as JPEG
but with a handful of components for the whole image instead of 8 by 8 blocks.
Pipeline
flowchart LR
U["Upload"] --> W["Worker: downscale to about 64 px"]
W --> E["Encode BlurHash (4 x 3)"]
E --> S["Store string with image metadata"]
S --> A["API returns blurhash in JSON"]
A --> D["Client decodes to 32 px canvas"]
D --> P["Scaled up as placeholder"]
P --> I["Real image replaces it"]
Colour space
BlurHash averages colours, and averaging must be done in linear light, not in the gamma-encoded sRGB bytes. Each sRGB
byte is converted to a linear value before encoding and back after decoding.
With , the linear value is
The inverse takes a linear value clamped to , and gives a byte
The reference code rounds by adding 0.5 and truncating.
Encoding
The encoder runs a DCT-like transform with components, where . 4 by 3 is the
usual choice. For an image of width and height , with the linear value of one colour channel, each
component is computed separately for the R, G and B channels:
is the average colour, the DC component. The other components are the AC components, which
describe how the colour varies across the image. The normalisation factor is 1 for and 2 for every other component.
Quantisation and packing
The string is written in base 83 with this alphabet, in this order:
A value is written most significant digit first, with a fixed number of digits. The fields appear in this order:
Size flag, 1 character: .
Maximum AC value, 1 character: , where is
the largest AC value. The decoder reads it back as . If there are no AC components, the
reference writes 0 and uses .
DC, 4 characters: the 24-bit sRGB average colour, , after converting each linear channel back
with .
Each AC component, 2 characters. Each channel is quantised to 19 levels,
, where
, and the three channels are packed as
. This fits in two base 83 digits because .
The total length is , which gives 28 characters for 4 by 3.
A detail worth knowing: in the TypeScript reference the maximum is taken as the plain maximum of the AC values, not of their absolute
values, and implementations differ on this detail. Both produce valid strings. Compare the output of two
implementations by decoding, not by comparing strings.
Decoding
The decoder reads and from the size flag, from the second character, and the DC and AC values.
Each AC channel value is
The DC value is converted from sRGB to linear. For an output of pixels, each pixel of each channel is
and is converted back to sRGB with . The output size does not have to match the original, which
is why decoding at 32 pixels and scaling up with the UI works.
punch is a contrast knob. In the formula, values above 1 exaggerate the AC components (more contrast) and values
below 1 soften them. The reference TypeScript decoder, however, sets punch = punch | 1. That bitwise OR first
truncates the value to an integer and then sets its lowest bit, so 0.5 and 1.5 become 1, 2 becomes 3 and 4
becomes 5. With that decoder only odd integers (1, 3, 5, …) take effect as given, and nothing below 1 is possible. If
you need another punch in JavaScript, scale the AC values yourself, or use an implementation that does not apply the
OR. The cost of decoding is
, which is trivial at 32 by 32.
Worked example
Take a 2 by 2 image where the left column is red and the right column is blue , and
encode it with , .
Linear values. and , so the left pixels are and the right pixels are
.
DC, . All cosines are 1 and , so the DC is the average of the four pixels:
.
AC, ., and is 1 for and 0 for . Only the left column
contributes, with weight 2 for each of its two pixels, so the red channel is . Green is 0,
and blue is zero up to floating point noise (about ). So .
Size flag., the character 1.
Maximum AC., so , the character ~, and
.
DC., so the colour is and the value is
, which is LjfL in four base 83 digits.
AC.. , so , which is
clamped to 18. For zero, . So and the packed value is
, which is |c.
The result is 1~LjfL|c: 8 characters, as expected from . Decoding it at 2 by 2 gives these pixels
in every row:
x=0: 255,0,188 x=1: 188,0,188
At the AC term is added to the DC; at the cosine is zero, so only the DC shows. The decoded image is only a rough hint of the original, which is all two components can represent. This is also what you see on real photos: very few components give a smooth blob of the dominant
colours.
A real hash, field by field
The running example is the Moby-Dick cover with id b81f, a 2400 by 3600 image. Its API record carries a BlurHash made from a 64 by 96 pixel reduction (exactly 2:3) with 3 columns and 4 rows of components.
The reference TypeScript encoder returns TIAxG?R,4pm#f8WB01t6?aN2j[t6 for those pixels. The function below unpacks the string with nothing but the rules of the previous sections:
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"
def b83(chars: str) -> int:
value = 0
for ch in chars:
value = value * 83 + ALPHABET.index(ch)
return value
def linear_to_srgb(v: float) -> int:
v = max(0.0, min(1.0, v))
return int((12.92 * v if v <= 0.0031308 else 1.055 * v ** (1 / 2.4) - 0.055) * 255 + 0.5)
def sign_pow(v: float, e: float) -> float:
return abs(v) ** e * (1 if v >= 0 else -1)
def fields(blurhash: str) -> None:
size = b83(blurhash[0])
nx, ny = size % 9 + 1, size // 9 + 1
assert len(blurhash) == 4 + 2 * nx * ny, "length does not match the size flag"
maximum = (b83(blurhash[1]) + 1) / 166
dc = b83(blurhash[2:6])
print(f"size flag {size} -> {nx} x {ny} components, {len(blurhash)} characters")
print(f"maximum AC value M = (q + 1) / 166 = {maximum:.4f}")
print(f"DC = {dc} -> sRGB ({dc >> 16}, {(dc >> 8) & 255}, {dc & 255})")
for index in range(1, nx * ny):
value = b83(blurhash[4 + 2 * index: 6 + 2 * index])
q = (value // 361, (value // 19) % 19, value % 19)
ac = tuple(round(sign_pow((x - 9) / 9, 2) * maximum, 4) for x in q)
i, j = index % nx, index // nx
print(f"AC({i},{j}) = {blurhash[4 + 2 * index: 6 + 2 * index]!r:5} -> q {q} -> {ac}")
The first character T is 29 in the alphabet, so the grid is (29 mod 9) + 1 = 3 columns by (29 div 9) + 1 = 4 rows, and 4 + 2 * 12 = 28 characters, as the length check requires.
The DC colour (93, 120, 154) is the average of the cover, a muted blue: the sea gradient dominates the picture. This single value is what a client sees before anything else is decoded.
Component (0, 1) is a vertical variation (half a cosine period over the height), and its blue value 0.1145 is the maximum M: blue changes strongly between the top and the bottom of the cover.
Components (2, 0) and (0, 2) are the strongest negative terms. The encoder took M from the largest signed value, the positive blue of (0, 1), so the larger negative red of (0, 2) could not be
represented: it is at the lowest of the 19 levels (q = 0) and is stored as -M. The next section shows that its true size is about -0.1265.
The 11 two-character AC values use only 22 of the 28 characters. The other six are the size flag, the maximum and the four DC characters. That is the whole image.
flowchart LR
S["28 characters"] --> F["Size flag: 3 x 4"]
S --> M["Maximum: M = 0.1145"]
S --> D["DC: average colour"]
S --> A["11 AC values, 19 levels each"]
A --> Q["signPow of (q - 9) / 9 with exponent 2, times M"]
D --> L["sRGB to linear"]
Q --> I["Inverse cosine sum per pixel"]
L --> I
I --> R["Linear to sRGB, 32 x 48 pixels"]
Same pixels, different strings
The clipped (0, 2) component is not a bug in the hash, it is a consequence of how the maximum is chosen, and it is where implementations disagree. The same 64 by 96 pixels and the same 3 by 4 grid, encoded by four libraries:
Implementation
String
Second character
blurhash 2.0.5 (TypeScript reference)
TIAxG?R,4pm#f8WB01t6?aN2j[t6
I
blurhash-java 1.0.0
TIAxG?R,4pm#f8WB01t6?aN2j[t6
I
blurhash-python 1.2.2 (wraps the C reference)
TKAxG?R,9Gm#f8WC0Lt6?GN1j[t6
K
blurhash 1.1.5 (the other Python package)
TKAxG?R,9Gm#f8WC0Lt6?GN1j[t6
K
The TypeScript and Java encoders agree character for character, and so do the two Python packages, but the two pairs differ in the maximum AC character (I is M = 0.1145, K is M = 0.1265). The first pair takes the largest signed AC value, so a strong
negative component is quantised against a maximum that it exceeds and is clipped: the C-based encoders' M of 0.1265 is the size of the red channel of (0, 2), which the TypeScript encoder stored as -0.1145 (the same
fields function applied to the second string shows (0, 2) as (-0.1265, -0.1, -0.0765), against (-0.1145, -0.1145, -0.0904) above). The second pair takes the largest absolute value, which leaves headroom for it. Decoded at 32 by 48 pixels, the two
pictures differ by 4.6 on a 0 to 255 scale on average (largest difference 45 in one channel), and both are about as far from the cover itself (a mean error of 37.5 against a box-reduced 32 by 48 copy of the cover) as each other. Both strings are valid and
every decoder reads either. What follows from it:
Never compare or deduplicate images by BlurHash string across services in different languages. Compare by decoding, or store the string with the implementation that wrote it.
Do not "fix" one side to match the other. The cost of clipping a component is below what the eye sees in a blurred placeholder.
Pick one encoder per platform and keep it for the lifetime of the stored strings, so a backfill produces strings that match the ones already stored.
Cost of encoding and decoding
Encoding costs time in proportion to the pixels, so encode from the reduction. In Node, the encoder took 2.3 ms for the 64 by 96 thumbnail with 3 by 4 components, and the Python wrapper took 2 ms for the same input, 300 ms for a 600 by 900 copy and 5.1 seconds for the
2400 by 3600 original, see the Python page. Decoding costs time in proportion to the output pixels times the number of components, so decode small and let the browser scale.
The TypeScript decoder on the same string:
Output size
Time per decode
32 x 48
0.29 ms
64 x 96
0.71 ms
320 x 480
17 ms
640 x 960
69 ms
Decoding at the size of the final image is 240 times slower than at 32 by 48 and gains nothing, since the result is blurry anyway.
Choosing the grid
The grid should follow the aspect ratio, so the hash does not spend its few components on a direction that has no detail. A function that matches the rule used on
the browser page:
def components_for(width: int, height: int, maximum: int = 4) -> tuple[int, int]:
ratio = width / height
if ratio >= 1:
return maximum, max(1, min(maximum, round(maximum / ratio)))
return max(1, min(maximum, round(maximum * ratio))), maximum
It returns (3, 4) for the 640 by 960 and the 2400 by 3600 cover (28 characters), (4, 2) for a 1920 by 1080 video cover (20 characters), (4, 4) for a square avatar (36 characters), and (4, 1) and (1, 4) for a 1200 by 300 banner and its
transpose (12 characters each). Python’s round rounds halves to even and JavaScript’s Math.round rounds them up, so inputs whose product is exactly a half can differ between a Python backend and the
JavaScript function. A bigger grid helps slowly: on the cover, a 1 by 1 hash had a mean error of 44.8, 3 by 4 had 37.3, 6 by 9 had 28.0 and 9 by 9 (166 characters) 27.9, see the table on the Python page.
Practical guidance
Components. 4 by 3 is the default and gives 28 characters. Adapt it to the aspect ratio: portrait covers do well with
3 by 4, banner images with 5 by 2. More components mean a longer string, a slower encode and a decode that
looks closer to the image but costs more. Past 6 by 6 the benefit is small.
Encode from a downscaled image. The encoder is , so run it on a thumbnail of about 64 to 100
pixels wide, not on a 24-megapixel original. The result is practically the same.
Store and return the string. It is 20 to 30 characters. Keep it next to the image in the database and return it
with the image in API JSON, see
responsive images and thumbnails.
Decode small. Decode to about 20 to 32 pixels wide and let the UI scale it up with smooth interpolation (canvas
CSS scaling or a blurred <img>). Never decode to the full image size.
Validate. A decoder should check that the length equals , as the reference does, before trusting
user-supplied strings.
Cover images of videos. Run the same encode on the extracted frame, see
video covers.
ThumbHash
ThumbHash by Evan Wallace is a similar placeholder format. It also encodes the
alpha channel, keeps the aspect ratio of the image inside the hash, and stores colour in a different colour space (it
uses a perceptual space and gives a slightly more detailed result in about 25 bytes). Its encoded form is binary, and is
usually written as base64. Use it when you need transparency, or when the frontend library you use supports it, for
example expo-image and
unlazy. Use BlurHash when you need the widest set of implementations. Measured on the cover (a 43 by 64 reduction, because ThumbHash takes at most 100 by 100 pixels),
thumbhash 0.1.1 produced 21 bytes, which is the 28 characters 2aUFNQQnh3Z3F7hoiH+EF2eAdvRo in base64: the same text length as the 3 by 4 BlurHash of the same pixels (TJAe8iWF4:iXa#WB02t6?aN2j[t6, also 28 characters), and
a base64 string can be stored in a bytea column at 21 bytes. It decoded to a 23 by 32 image, because the aspect ratio is recovered from the hash (approximately, 0.714 for a true 0.672 here) rather than being carried by the client, and
that is the practical advantage: a client needs no extra width and height to size the placeholder.