MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / tensor_to_pil

Function tensor_to_pil

eval_code/recons/models/pi3/utils/basic.py:86–102  ·  view source on GitHub ↗

Converts a PyTorch tensor to a PIL image. Automatically moves the channel dimension (if it has size 3) to the last axis before converting. Args: tensor (torch.Tensor): Input tensor. Expected shape can be [C, H, W], [H, W, C], or [H, W]. Returns: PIL.Image: The

(tensor)

Source from the content-addressed store, hash-verified

84
85
86def tensor_to_pil(tensor):
87 """
88 Converts a PyTorch tensor to a PIL image. Automatically moves the channel dimension
89 (if it has size 3) to the last axis before converting.
90
91 Args:
92 tensor (torch.Tensor): Input tensor. Expected shape can be [C, H, W], [H, W, C], or [H, W].
93
94 Returns:
95 PIL.Image: The converted PIL image.
96 """
97 if torch.is_tensor(tensor):
98 array = tensor.detach().cpu().numpy()
99 else:
100 array = tensor
101
102 return array_to_pil(array)
103
104
105def array_to_pil(array):

Callers

nothing calls this directly

Calls 1

array_to_pilFunction · 0.70

Tested by

no test coverage detected