Resize¶
| Fact | Value |
|---|---|
| Also called | Image size, Resample |
| Status | Implemented and tested |
| Family | Utilities |
| Engine | Browser |
| Type | Linear, geometric, separable resampling |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | the whole image |
| Code | web/src/core/image/resample.ts, resizeImageTo |
| Operator code | web/src/core/image/resample.ts, resampleKernel |
Overview¶
Resize changes the size of the image in pixels. Open it with Image > Resize (Ctrl+Alt+I or Cmd+Alt+I). Four methods decide how the new pixels are mixed from the old ones: nearest neighbor, bilinear, bicubic and Lanczos. When the image shrinks, the method averages the pixels each new pixel covers, so fine patterns do not turn into false coarse ones.
How it works¶
- Find where the center of each new pixel falls in the old image.
- Mix the old pixels near that spot, with weights from the chosen kernel that add up to 1.
- When shrinking, widen the kernel by the shrink factor, so it averages all the covered pixels.
- Do this along the rows, then along the columns.
Nearest neighbor skips the mixing and copies the old pixel under the new center.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Source value, premultiplied by alpha
- \(I_{\text{out}}\): Output value, before it is divided by alpha
- \(x\): Output pixel index
- \(i\): Source pixel index
- \(u\): Source position of the center of output pixel x, in pixel-center units
- \(s\): Source size divided by output size
- \(f\): Kernel stretch, 1 when enlarging and s when shrinking
- \(k\): The kernel of the chosen method
Pixel i covers the interval from i to i + 1, so its center is i + 1/2. The center of output pixel x falls at the source position u. The kernel k weights the source pixels near u, and dividing by the sum makes the weights add up to 1. When shrinking, the kernel is f times wider, so each new pixel also averages the pixels it covers. That prevents aliasing. The same step runs along rows, then along columns.
The kernels are mathematical definitions, the same as in Pillow:
where \(x\) is the distance in source pixels, after the stretch by \(f\), and \(\operatorname{sinc}(t) = \sin(\pi t) / (\pi t)\). The bicubic kernel is the cubic convolution of Keys (1981) with \(a = -1/2\).
Shrinking without the stretch \(f\) would sample the image too sparsely. Patterns finer than the new pixel spacing would fold back as false coarse patterns, called aliasing. Stretching the kernel by the shrink factor makes it a low-pass filter for the new spacing, so those patterns are averaged away first.
Kernel or operator¶
Kernel values at a few distances, from the code.
| x | 0 | 0.5 | 1 | 1.5 | 2 | 2.5 |
|---|---|---|---|---|---|---|
| Bilinear | 1 | 0.5000 | 0 | 0 | 0 | 0 |
| Bicubic | 1 | 0.5625 | 0 | -0.0625 | 0 | 0 |
| Lanczos | 1 | 0.6079 | 0 | -0.1351 | 0 | 0.0243 |
The kernels are symmetric, so k(-x) = k(x). Bicubic and Lanczos are negative between 1 and 2, which sharpens and makes thin halos.
Algorithm¶
- Multiply the colors by alpha.
- For every output column, find the source position of its center and the normalized kernel weights.
- Mix the source pixels of each row with those weights.
- Do the same for every output row, on the result of the first pass.
- Divide the colors by alpha and round once to 8 bits.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Width width |
512 | 1 to 32768 px | The new width. The dialog starts with the current width. |
Height height |
512 | 1 to 32768 px | The new height. The dialog starts with the current height. |
Keep proportions keepRatio |
on | On or off | Change the other side to keep the shape of the image. |
Method method |
Bicubic | Nearest neighbor, Bilinear, Bicubic, Lanczos | The kernel that mixes source pixels into each new pixel. |
Implementation¶
resizeImageTo in web/src/core/image/resample.ts precomputes, for each output column and each output row, the first source pixel and the normalized weights. Then it runs the two passes in floating point, with colors premultiplied by alpha, and rounds once at the end. Near the image edge, source pixels outside the image are left out, and the remaining weights are divided by their sum. The selection is resized with the image, with bilinear weights.
Resize changes the image size, so it does not run through the tiled filter engine, and it has no live preview. Its catalog entry names the command image.resize, which the Library, the Properties panel and Ctrl+Enter open.
Worked example¶
| Output x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Source position u | -0.25 | 0.25 | 0.75 | 1.25 | 1.75 | 2.25 | 2.75 | 3.25 |
| Value | 0 | 0 | 0 | 64 | 191 | 255 | 255 | 255 |
The row 0, 0, 255, 255 enlarged to twice its width with bilinear weights. Output pixel 3 lands at u = 1.25, between source pixels 1 (value 0) and 2 (value 255). Its weights are 0.75 and 0.25, so the value is 0.25 x 255 = 63.75, which rounds to 64. At the ends, u falls outside the image and the nearest pixel takes the full weight.
Complexity¶
- Time: \(O(W_o\,H_i\,k_x + W_o\,H_o\,k_y)\)
- Memory: \(O(W_o\,H_i)\)
W and H are widths and heights, i for input and o for output. k_x and k_y are the kernel widths in pixels, which grow when shrinking.
Artifacts and limitations¶
Limitations
- Enlarging cannot add detail. The image gets bigger, not sharper.
- Bicubic and Lanczos have negative weights, so they make thin halos at sharp edges.
Artifacts
- Halos and slight ringing with bicubic and Lanczos.
- Blocky pixels with nearest neighbor.
- Soft results with bilinear.
Tests¶
The reference is a NumPy version of the same definitions in tools/fixtures_geometry.py. Before writing it, the fixture script checks it against Pillow's resize on an opaque image. Pillow rounds to 8 bits between its two passes, so the check uses a twin of the reference that does the same, and allows 1 level. Nearest neighbor must match Pillow exactly. The test compares all four methods for four sizes on an image with transparent pixels, within 1 level.
References¶
- Keys, R. Cubic Convolution Interpolation for Digital Image Processing. IEEE Transactions on Acoustics, Speech, and Signal Processing 29(6), 1153 to 1160, 1981. DOI 10.1109/TASSP.1981.1163711.
- Pillow documentation, Image.resize and the resampling filters. https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize