Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

`getprojection()` and `histogram()` of I;16 images read the first `width` bytes of each row instead of the pixels

Open jiren-the-gray opened this issue 3 weeks ago • 3 comments

What did you do?

Called getprojection() and histogram() on small I;16 and I;16B images (code below).

What did you expect to happen?

getprojection() returns, for each axis, which positions hold a non-zero pixel. So I;16 [0, 1] should give ([0, 1], [1]) and [256, 0] should give ([1, 0], [1]), as getbbox() and an "I" image with the same values do.

histogram() returns 256 bins for these images. Under any mapping of 16-bit values to the bins, each pixel is counted by its own value, two pixels with the same value share a bin, and the result does not depend on the image's shape. Converting to "I" first gives that.

What actually happened?

([0, 0], [0])
([0, 1], [1])
([0, 1], [1])
(1, 0, 2, 1)
{0: 2}
{0: 2}
{0: 1, 5: 1}
{5: 2}
{0: 1, 255: 1}
  1. getprojection() misses the non-zero pixel of [0, 1], and for [256, 0] (I;16) and [5, 0] (I;16B) it reports the non-zero pixel in column 1 instead of column 0. getbbox() on the same image is right.
  2. histogram() gives the same result for [0, 65535] and [0, 0]: the second pixel is never read. The two pixels of a 2x1 [5, 5] image land in bins 0 and 5, while the same pixels as a 1x2 image land in bin 5. The "I" conversion counts [0, 65535] as bins 0 and 255.

The results match reading the first width bytes of each row as 8-bit pixels: the low and high bytes of the left half of the pixels, and nothing of the right half. With a mask, the mask selects bytes too: histogram(mask) of [0, 65535] with an "L" mask [0, 255] counts one pixel in bin 0 (the high byte of pixel 0). histogram(extrema=...) gives the same counts, since extrema is used only for "I" and "F". entropy() reads the same histogram and returns 0.0 for [0, 65535] (1.0 after convert("I")). I;16L and I;16N give the same output as I;16; I;16B shows the same byte scan with its bytes in big-endian order (the 1x2 [5, 5] image lands in bin 0, not bin 5). Pillow 12.0.0 gives the same output, except that its getbbox() also returned None before #9768.

Possible cause: an I;16 image has 2-byte pixels but gets the image8 alias (Storage.c#L125-L131, L224-L229). ImagingGetProjection runs GETPROJ(image8, 0xff) over x < xsize bytes (GetBBox.c#L153-L168), while ImagingGetBBox a few lines above scans I;16 rows as UINT16 since #9768 (L113-L116). ImagingGetHistogram does histogram[in[x]]++ over the same bytes, without a mask (Histo.c#L142-L150) and with one (L100-L111). For getprojection() the getbbox() branch would fit. For histogram(), handling I;16 like "I" (scaled by the extrema) would match what convert("I").histogram() returns.

What are your OS, Python and Pillow versions?

  • OS: Debian 12 in Docker (official Python image, Pillow wheel from PyPI) on Ubuntu 22.04.5, x86_64
  • Python: 3.12.16
  • Pillow: 13.0.0 (also 12.0.0)
--------------------------------------------------------------------
Pillow 13.0.0
Python 3.12.16 (main, Oct 28 2026, 02:01:35) [GCC 12.2.0]
--------------------------------------------------------------------
Python executable is /usr/local/bin/python3
System Python files loaded from /usr/local
--------------------------------------------------------------------
Python Pillow modules loaded from /usr/local/lib/python3.12/site-packages/PIL
Binary Pillow modules loaded from /usr/local/lib/python3.12/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 13.0.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.14.3
--- LITTLECMS2 support ok, loaded 2.19
--- WEBP support ok, loaded 1.6.0
--- AVIF support ok, loaded 1.4.2
--- JPEG support ok, compiled for libjpeg-turbo 3.2.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.4
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13, compiled for zlib-ng 2.3.3
--- LIBTIFF support ok, loaded 4.7.2
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
from PIL import Image


def i16(values, mode="I;16", size=None):
    im = Image.new(mode, size or (len(values), 1))
    im.putdata(values)
    return im


def bins(h):
    return {i: n for i, n in enumerate(h) if n}


print(i16([0, 1]).getprojection())  # expected ([0, 1], [1])
print(i16([256, 0]).getprojection())  # expected ([1, 0], [1])
print(i16([5, 0], "I;16B").getprojection())  # expected ([1, 0], [1])
print(i16([0, 1]).getbbox())  # (1, 0, 2, 1), correct

print(bins(i16([0, 65535]).histogram()))
print(bins(i16([0, 0]).histogram()))
print(bins(i16([5, 5]).histogram()))  # 2x1
print(bins(i16([5, 5], size=(1, 2)).histogram()))  # 1x2, same pixels
print(bins(i16([0, 65535]).convert("I").histogram()))

jiren-the-gray avatar Nov 06 '26 19:11 jiren-the-gray

Hi. You're using AI to make these reports, yes?

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.histogram

The histogram is returned as a list of pixel counts, one for each pixel value in the source image. Counts are grouped into 256 bins for each band, even if the image has more than 8 bits per band.

I'm not convinced that the current behaviour of grouping I into 256 bins is the most helpful solution. If you're using histogram() with these modes, I'd be keen to hear what you think about our strategy.

radarhere avatar Nov 06 '26 20:11 radarhere

Hi @radarhere

Yes, I used AI to help write this report, but I ran the reproducer and checked the output myself before filing. I will include an AI disclosure in future reports.

On the histogram strategy: I don't use histogram() on I images in my own work, so I can't offer a use case. My work is primarily focused on testing. But, I found one behavior kind of unique: an image with a single value gets an all-zero histogram, because the minimum equals the maximum and no pixel is counted (Image.new("I", (2, 1), 5).histogram() sums to 0). I;16 now behaves the same way. numpy.histogram, which also takes its default range from the data's minimum and maximum, widens an empty range by 0.5 on each side, so all values land in the middle bin.

jiren-the-gray avatar Nov 10 '26 04:11 jiren-the-gray

Interesting. This unique behaviour is actually being discussed in https://github.com/python-pillow/Pillow/pull/10475 at the moment.

radarhere avatar Nov 10 '26 04:11 radarhere