Skip to content

Converting F images to I;16 clips values to 255 #10348

Closed

Description

@usmanmateen

What did you do?

Converted a float (F) image, such as a depth map in millimetres or a 32-bit float TIFF, to 16-bit so it can be saved as a 16-bit PNG:

import numpy as np
from PIL import Image

depth = np.array([[0.0, 254.5, 255.0, 1000.0, 40000.0, 65535.0, 70000.0]], np.float32)
im = Image.fromarray(depth, "F")
for mode in ("I", "I;16", "I;16L", "I;16B"):
    print(mode, np.asarray(im.convert(mode).convert("I")).tolist())

What did you expect to happen?

I;16 holds 0 to 65535, so the values should only be clipped to that range, as im.convert("I").convert("I;16") does:

I     [[0, 254, 255, 1000, 40000, 65535, 70000]]
I;16  [[0, 254, 255, 1000, 40000, 65535, 65535]]

What actually happened?

Every value above 255 becomes 255:

I     [[0, 254, 255, 1000, 40000, 65535, 70000]]
I;16  [[0, 254, 255, 255, 255, 255, 255]]
I;16L [[0, 254, 255, 255, 255, 255, 255]]
I;16B [[0, 254, 255, 255, 255, 255, 255]]

Convert.c has no F to I;16* converter, so Image.convert() falls back to converting through the base mode L first, which clips to 0..255. This is the same fallback that #9931 fixed for I;16N to I and F. I can open a PR that adds F to I;16, I;16L, I;16B and I;16N converters, clipping to 0..65535 and truncating like the existing F to I and F to L conversions.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 26.04 (WSL2), x86-64
  • Python: 3.12.13
  • Pillow: 13.0.0
--------------------------------------------------------------------
Pillow 13.0.0
Python 3.12.13 (main, Aug  7 2026, 02:25:39) [Clang 22.1.3 ]
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 13.0.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1, compiled for zlib-ng 2.3.3
--- LIBTIFF support ok, loaded 4.7.2
--------------------------------------------------------------------

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions