Skip to content

Support 16-bit images in ImageEnhance - #10549

Merged
radarhere merged 4 commits into
python-pillow:mainfrom
TheVicky1:enhance-16bit-images
Nov 21, 2026
Merged

radarhere merged 4 commits into
python-pillow:mainfrom
TheVicky1:enhance-16bit-images

Conversation

@TheVicky1

@TheVicky1 TheVicky1 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #7397.

Changes:

  1. src/PIL/ImageEnhance.py:
    • Contrast: For 16-bit images (I;16*), calculated the image mean using box resampling (image.resize((1, 1), Image.Resampling.BOX).getpixel((0, 0))) to preserve full 16-bit precision without clipping values $> 255$.
    • Color: For 16-bit images, maintained intermediate mode as image.mode so that grayscale images are cleanly preserved without 8-bit quantization.
  2. Tests & Docs:
    • Added test coverage in Tests/test_imageenhance.py verifying Color, Brightness, Contrast, and Sharpness on 16-bit images (I;16, I;16B, I;16L, I;16N).
    • Added release note entry in docs/releasenotes/13.1.0.rst.

Comment thread src/PIL/ImageEnhance.py Outdated
if 0 in image.size:
mean = 0
else:
value = image.resize((1, 1), Image.Resampling.BOX).getpixel((0, 0))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose BOX here?

@radarhere radarhere changed the title Support 16-bit images in ImageEnhance and Image.blend Support 16-bit images in ImageEnhance Nov 18, 2026
@radarhere

Copy link
Copy Markdown
Member

I separated out the blend change into #10551

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 668 untouched benchmarks
⏩ 352 skipped benchmarks1


Comparing TheVicky1:enhance-16bit-images (12e4c61) with main (0fb3df5)

Open in CodSpeed

Footnotes

  1. 352 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@TheVicky1
TheVicky1 force-pushed the enhance-16bit-images branch from a398d5f to b991754 Compare November 19, 2026 06:46
@TheVicky1

Copy link
Copy Markdown
Contributor Author

ImageStat.Stat calculates the mean using image.histogram(), which is limited to 256 bins. For 16-bit images (I;16) with pixel values exceeding 255, this bins/quantizes values into 8-bit space and produces an incorrect mean (e.g., an I;16 image where all pixels are 1000 yields mean = [117.5]).

Resampling to 1x1 with Image.Resampling.BOX performs exact area-averaging over the entire image in C without 8-bit quantization or clipping. Unlike filters such as NEAREST (which only picks a single sample pixel) or higher-order convolution filters, BOX applies equal weighting across all pixels in the bounding box, providing the true arithmetic average.

Also, now that #10551 has landed, I have rebased this branch on main.

@TheVicky1
TheVicky1 requested a review from radarhere November 19, 2026 06:49
@TheVicky1
TheVicky1 force-pushed the enhance-16bit-images branch 2 times, most recently from b1762b8 to fbe6bd8 Compare November 19, 2026 07:28
@TheVicky1
TheVicky1 force-pushed the enhance-16bit-images branch from fbe6bd8 to 2e060ec Compare November 19, 2026 08:35
@radarhere
radarhere force-pushed the enhance-16bit-images branch from 969e86e to 12e4c61 Compare November 21, 2026 07:54
@radarhere
radarhere enabled auto-merge (squash) November 21, 2026 07:57
@radarhere
radarhere disabled auto-merge November 21, 2026 07:57
@radarhere
radarhere enabled auto-merge (squash) November 21, 2026 07:57
@radarhere
radarhere merged commit 7610556 into python-pillow:main Nov 21, 2026
55 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageEnhance is not working on 16 bit images

2 participants