Skip to content

Fix pil_to_tensor() to support I;16 mode PIL images - #9661

Closed
Anuragcap wants to merge 1 commit into
pytorch:mainfrom
Anuragcap:fix/pil_to_tensor_i16_support
Closed

Fix pil_to_tensor() to support I;16 mode PIL images#9661
Anuragcap wants to merge 1 commit into
pytorch:mainfrom
Anuragcap:fix/pil_to_tensor_i16_support

Conversation

@Anuragcap

Copy link
Copy Markdown

Fixes #8188.

pil_to_tensor() converted PIL images with a raw np.array(pic, copy=True) call, which inherits whatever dtype PIL returns for the image's mode. For I;16 mode (16-bit grayscale), PIL returns uint16, which PyTorch has no native tensor type for, causing a TypeError.

to_tensor() already handles this correctly via a mode_to_nptype mapping. This PR applies the same mapping to pil_to_tensor().

Test

Added test_pil_to_tensor_i16 in test/test_transforms.py, verifying that an I;16 mode image converts to an int16 tensor with correct shape and values.

Fixes pytorch#8188. pil_to_tensor() converted PIL images to numpy arrays
without mapping mode-specific dtypes, so I;16 mode images (which
PIL exposes as uint16) failed since PyTorch has no native uint16
tensor type. This applies the same mode_to_nptype mapping that
to_tensor() already uses.
@pytorch-bot

pytorch-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9661

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Sep 9, 2026

Copy link
Copy Markdown

Hi @Anuragcap!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@Anuragcap Anuragcap closed this Sep 9, 2026
@Anuragcap
Anuragcap deleted the fix/pil_to_tensor_i16_support branch September 9, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pil_to_tensor() doesn't work for PIL Image with I;16 mode

1 participant