Skip to content

Image.BOX resampling skips input pixels #9961

Closed as duplicate
Closed as duplicate

Description

@mzient

What did you do?

I resized an image of size 5x19 down to 5x10 with resample=Image.BOX.
The input, upscaled 10x for clarity
Image

What did you expect to happen?

I expected each input pixel to correspond to one output pixel, as per documentation.

What actually happened?

The middle row didn't contribute to the output at all - it was totally left out (see result).
Image

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04, Linux 6.8.0-138-generic
  • Python: 3.10.12
  • Pillow: 9.0.1 and 12.3.0 (tested on both, same result)
--------------------------------------------------------------------
Pillow 12.3.0
Python 3.10.12 (main, Jul 15 2026, 23:40:17) [GCC 11.4.0]
--------------------------------------------------------------------
Python executable is /usr/bin/python3
System Python files loaded from /usr
--------------------------------------------------------------------
Python Pillow modules loaded from /home/michalz/.local/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /home/michalz/.local/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 12.3.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.11.1
*** LITTLECMS2 support not installed
*** WEBP support not installed
*** AVIF support not installed
--- JPEG support ok, compiled for libjpeg-turbo 2.0.6
*** OPENJPEG (JPEG2000) support not installed
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.1-motley
--- LIBTIFF support ok, loaded 4.6.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------

Repro:

import PIL.Image
import numpy as np

def checkerboard(w, h):
    a = (np.arange(w).reshape((1, w, 1)) ^ np.arange(h).reshape((h, 1, 1))) & 1
    return a.astype(np.uint8) * np.uint8([255, 255, 255])

c = checkerboard(5, 19)
c[:9,:,1:3] = 0  # make top part red...
c[9,:,::2] = 0  # ...middle row green...
c[10:,:,0:2] = 0  # ...and bottom part blue
input = PIL.Image.fromarray(c)
resized = input.resize((5, 10), resample=PIL.Image.BOX)

Activity

  1. radarhere commented on Sep 4, 2026

    @radarhere
    Member

    Hi. This seems like a duplicate of #9939

    #9949 is a suggested fix. In your case, it would generate (fuzziness is because I've increased the width and height in HTML)
    Image

  2. mzient commented on Sep 4, 2026

    @mzient
    Author

    Thanks, @radarhere!
    This indeed seems like a duplicate. I'm currently working on an accelerated Pillow fork and hit this issue in testing.
    I currently get this output - is it the same as the one from the proposed PR? If we're about to make an arbitrary decision on where the pixels contribute, I'd like to be in sync with future Pillow versions.
    Image

  3. radarhere commented on Sep 4, 2026

    @radarhere
    Member

    Yes, the image you just posted is the same as the one I posted from the proposed PR.

  4. AlttiRi commented on Sep 4, 2026

    @AlttiRi

    Yes, it's the same problem.

    The visual difference is visible at the image center horizontal line:
    Image Image Image

    (I downscaled your gray-scaled image from 5x19 to 5x10 (by 1.9 times by height), then the result upscaled by 25 times. PIL's result is the second one.)

    The interested moment here is that, when I downscale your 26x100 pixel version image by 1.9 from 50x190 to 26x100, the difference is not at the center line, but bottom:

    Image Image Image
    (The result is upscaled by 5 times.)

    So, it confirms, what the problem appears not only on the center lines (vert/horz).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions