Skip to content

Commit dd84bac

Browse files
author
The TensorFlow Datasets Authors
committed
Fix PIL image conversion for uint16 multi-channel arrays.
PiperOrigin-RevId: 911782033
1 parent 1dac9e9 commit dd84bac

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

tensorflow_datasets/core/utils/image_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,19 @@ def create_thumbnail(
177177
if use_colormap: # Apply the colormap first as it modify the shape/dtype
178178
ex = apply_colormap(ex)
179179

180-
_, _, c = ex.shape
180+
# PIL.Image.fromarray doesn't support uint16 for >1 channels.
181+
# https://github.com/python-pillow/Pillow/blob/11.0.0/src/PIL/Image.py#L3225
182+
if ex.dtype == np.uint16:
183+
ex = (ex / 257).astype(np.uint8)
184+
185+
c = ex.shape[-1] if ex.ndim == 3 else 1
181186
postprocess = _postprocess_noop
182187
if c == 1:
183188
ex = ex.squeeze(axis=-1)
184189
mode = 'L'
185-
elif ex.dtype == np.uint16:
186-
mode = 'I;16'
187-
postprocess = _postprocess_convert_rgb
188190
else:
189191
mode = None
192+
postprocess = _postprocess_convert_rgb
190193
img = PIL_Image.fromarray(ex, mode=mode)
191194
img = postprocess(img)
192195
if default_dimensions:

0 commit comments

Comments
 (0)