pytorch / pytorch/vision

About converting PIL Image to PyTorch Tensor

Open
#2,989 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module: transforms
Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

About converting PIL Image to PyTorch Tensor

I use PIL open an image:

pic = Image.open(...).convert('RGB')

Then I want to convert it to tensor, I have read torchvision.transforms.functional, the function to_tensor use the following way:

img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
img = img.view(pic.size[1], pic.size[0], len(pic.getbands()))

Why not use the following method directly:

img = torch.from_numpy(np.array(pic))

I want to know what is the difference, is it because of the difference in efficiency?

The following is the code snippet for to_tensor to handle PIL Image

# handle PIL Image
    if pic.mode == 'I':
        img = torch.from_numpy(np.array(pic, np.int32, copy=False))
    elif pic.mode == 'I;16':
        img = torch.from_numpy(np.array(pic, np.int16, copy=False))
    elif pic.mode == 'F':
        img = torch.from_numpy(np.array(pic, np.float32, copy=False))
    elif pic.mode == '1':
        img = 255 * torch.from_numpy(np.array(pic, np.uint8, copy=False))
    else:
        img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))

    img = img.view(pic.size[1], pic.size[0], len(pic.getbands()))
    # put it from HWC to CHW format
    img = img.permute((2, 0, 1)).contiguous()
    if isinstance(img, torch.ByteTensor):
        return img.float().div(255)
    else:
        return img

cc @vfdev-5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with torchvision.transforms.functional.to_tensor and the shown PIL-handling branch. Compare its dtype, layout, mode handling, and normalization with np.array(pic), then document the relevant differences or clarify the expected usage; the issue is done when the question has an authoritative explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
computer-vision, machine-learning
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.