Fix integer overflow in 16-bit resampling by hayatoikoma · Pull Request #9480 · python-pillow/Pillow

@hayatoikoma hayatoikoma changed the title Fix integer overflow/byte corruption in 16-bit (I;16) resampling by clamping float sums Fix integer overflow in 16-bit resampling

Mar 20, 2026

@hayatoikoma

This commit fixes a bug in Resample.c where downsampling 16-bit
images (I;16) using filters with negative lobes (such as
Image.Resampling.LANCZOS) could result in byte corruption.

Because Lanczos weighting can create overshoots (ringing artifacts)
near sharp edges, the accumulated floating-point sum can sometimes
exceed the 16-bit maximum (65535) or fall below zero. Previously,
these out-of-bounds values were not correctly clamped before being
cast or packed into the 16-bit output buffer, leading to integer
overflow/underflow and corrupted pixels.

This update correctly clamps the accumulated float values to the
[0, 65535] range for I;16 images during resampling.

@radarhere

wiredfool

@hayatoikoma