Bayer 2, Bayer 4, Bayer 8 Dithering: The Pattern-Based Image Transformation Guide

Bayer 2x2, 4x4 and 8x8 dithering matrices with every value written out, how ordered dithering works, the threshold formula, Python code and a 4K Bayer pattern.

If you’ve been searching things like “bayer dither algorithm”, “bayer 4x4 dithering”, or comparing it with error diffusion methods, you’ve probably noticed one big difference.

Bayer dithering does not spread error.

It uses patterns.

And that changes everything about how the image looks.

What is Bayer Dithering

The Bayer dithering is a technique that converts images into limited color palettes using a fixed threshold matrix instead of error diffusion.

Instead of pushing error to nearby pixels, it compares each pixel against a repeating pattern.

Simple way to think about it:

Every pixel is judged using a small grid. That grid repeats across the image and creates a structured texture.

Ordered Dithering vs Error Diffusion

Before going deeper, here’s the key difference:

Bayer falls into:

Ordered dithering

This makes it:

Bayer Matrix Basics

At the core of Bayer dithering is a matrix.

This matrix defines threshold values that decide whether a pixel becomes light or dark.

The general transformation looks like this:

For each pixel I(x,y):

Bayer 2x2 Dithering

This is the simplest version.

Matrix:

0  2
3  1

This creates a very coarse pattern.

Characteristics:

Because the matrix is small, patterns repeat quickly, making the texture obvious.

Bayer 4x4 Dithering

This is where things start getting more usable.

Matrix:

 0   8   2  10
12   4  14   6
 3  11   1   9
15   7  13   5

Characteristics:

This is one of the most commonly used Bayer matrices.

Bayer 8x8 Dithering

This is a much larger matrix with 64 values.

It is recursively built from the 4x4 matrix and gives 64 threshold levels instead of 16.

Matrix:

 0  32   8  40   2  34  10  42
48  16  56  24  50  18  58  26
12  44   4  36  14  46   6  38
60  28  52  20  62  30  54  22
 3  35  11  43   1  33   9  41
51  19  59  27  49  17  57  25
15  47   7  39  13  45   5  37
63  31  55  23  61  29  53  21

Characteristics:

This is where Bayer starts competing visually with error diffusion methods.

How Bayer matrices are generated

Bayer matrices are built using a recursive pattern.

You start with a small matrix and expand it using a formula.

This process is tied to:

Matrix transformation

Each level increases resolution while preserving structure.

That is why:

The rule for going from an n×n matrix M to a 2n×2n one is:

M(2n) = | 4M + 0   4M + 2 |
        | 4M + 3   4M + 1 |

Apply it to the 2x2 matrix and you get the 4x4 above. Apply it to the 4x4 and you get the 8x8.

To use a matrix as thresholds, turn each value into a number between 0 and 1:

threshold(x, y) = (M[y mod n][x mod n] + 0.5) / n²

A pixel with brightness above its threshold becomes white, otherwise black.

Image transformation process

Here is how Bayer dithering transforms an image:

  1. Normalize pixel values between 0 and 1
  2. Scale matrix values to the same range
  3. Compare each pixel with its corresponding matrix cell
  4. Assign output color

This is a form of:

Spatial color quantization

But instead of distributing error, it distributes thresholds.

Bayer dithering in Python

This builds a Bayer matrix of any size (2, 4, 8, 16) and applies it to a greyscale image. It needs NumPy and Pillow.

import numpy as np
from PIL import Image

def bayer(n):
    m = np.array([[0, 2], [3, 1]])
    while m.shape[0] < n:
        m = np.block([[4*m, 4*m + 2], [4*m + 3, 4*m + 1]])
    return m

def dither(path, n=8, out="dithered.png"):
    img = np.asarray(Image.open(path).convert("L"), dtype=float) / 255
    m = (bayer(n) + 0.5) / (n * n)
    h, w = img.shape
    t = np.tile(m, (h // n + 1, w // n + 1))[:h, :w]
    Image.fromarray(((img > t) * 255).astype(np.uint8)).save(out)

Change n to switch between Bayer 2, Bayer 4 and Bayer 8.

Bayer 8x8 pattern in 4K

A lot of people want the Bayer pattern itself as an image, to use as a texture, a threshold map in a shader, or an overlay in Photoshop or After Effects.

This makes a 3840×2160 PNG of the 8x8 pattern. Each matrix cell is scaled up to 8×8 pixels so the grid stays crisp:

def pattern_4k(n=8, cell=8, out="bayer-8x8-4k.png"):
    m = (bayer(n) * 255 / (n * n - 1)).astype(np.uint8)
    m = np.kron(m, np.ones((cell, cell), dtype=np.uint8))
    tile = np.tile(m, (2160 // m.shape[0] + 1, 3840 // m.shape[1] + 1))
    Image.fromarray(tile[:2160, :3840]).save(out)

Set cell=1 for a pixel-exact pattern, or n=4 for a 4x4 one. The pattern tiles seamlessly, so it can be repeated at any size.

If you would rather not run code, the Bitgrain dither tool applies Bayer 2x2, 4x4 and 8x8 to any photo in the browser and exports a PNG.

Bayer vs Floyd Steinberg

Comparing with Floyd–Steinberg dithering algorithm:

Bayer

Floyd Steinberg

Where Bayer dithering is used

You’ll find Bayer dithering in:

Because it is computationally cheap, it works great where performance matters.

When to use Bayer 2, 4, or 8

Use Bayer 2x2 when:

Use Bayer 4x4 when:

Use Bayer 8x8 when:

Final thoughts

Bayer dithering is not trying to hide its structure.

It embraces it.

That is why people searching for:

often end up using it for style, not just compression.

A small perspective

Error diffusion tries to simulate reality.

Bayer creates its own.

And depending on what you are building, that control over pattern can be more powerful than realism.

Keep reading