Skip to content

Pixelate

Fact Value
Also called Mosaic, Pixelation, Block averaging
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Linear, spatial, block mean
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/artistic/effects.ts, pixelateFilter
Operator code web/src/core/filters/artistic/effects.ts, pixelatePlane
Channels RGB, Brightness only

Overview

Pixelate replaces each square block of the image with its mean color, so the image looks made of large pixels. It hides detail, and it gives a retro, low-resolution look.

Three panels: a Siemens star with fine spokes, the star in blocks of 4 pixels, and the star in blocks of 8 pixels, where the center turns into a coarse pattern. Three panels: a Siemens star with fine spokes, the star in blocks of 4 pixels, and the star in blocks of 8 pixels, where the center turns into a coarse pattern.
The Siemens star, and pixelate with blocks of 4 and 8 pixels.

How it works

  1. Cut the image into square blocks, starting at the top-left corner.
  2. Compute the mean of each block.
  3. Fill the block with its mean.

Mathematical foundation

\[ I_{\text{out}}(p) = \frac{1}{|B(p)|} \sum_{q \in B(p)} I(q), \qquad n = 8 \]

The formula shows the default parameter values.

where

  • \(B(p)\): The block that holds pixel p
  • \(n\): The block size

B(p) is the block that holds pixel p. Blocks start at the top-left corner of the image and are n by n pixels. At the right and bottom edges, blocks can be smaller, and they average the pixels they have. Every pixel of a block gets the same mean. This is a box filter sampled once per block and repeated. Pillow's reduce followed by nearest-neighbor enlarging gives the same means.

Averaging a block removes all detail smaller than the block. But the blocks sample the image only once per block, so detail near the block size does not vanish cleanly. It turns into false patterns, which is aliasing. Near the center of the Siemens star, where the spokes are finest, the blocks show patterns that are not in the star.

Kernel or operator

The Inspector shows the block mean with the current block size.

Algorithm

  1. Cut the image into n by n blocks from the top-left corner.
  2. Compute the mean of each block.
  3. Fill each block with its mean.

Parameters

Parameter Default Values Meaning
Block size size 8 2 to 256 px The side n of each square block.
Output mapping outputMapping Clip to 0 to 255 Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

pixelateFilter in web/src/core/filters/artistic/effects.ts calls pixelatePlane. It is a global filter, so the blocks line up across the whole image and its tiles.

Worked example

Block size Blocks PSNR (dB)
2 6400 19.89
4 1600 15.40
8 400 12.23
16 100 10.17

The Siemens star of the figure, 160 by 160 pixels. The PSNR compares the result with the star. Larger blocks keep less of the fine detail.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Each pixel is read once and written once.

Artifacts and limitations

Limitations

  • Small blocks do not hide enough. Use large ones to hide faces or text.

Artifacts

  • Hard edges between blocks.
  • False patterns from fine detail.

Tests

The fixture cases compare every pixel with NumPy block means for block sizes 2, 3, 4 and 8. These leave smaller blocks at the edges of the 13 by 10 test plane. Before the cases are written, the block means are checked against Pillow's reduce.

References