MCPcopy Create free account
hub / github.com/Ropedia/SpatialBench / tensor_to_pil

Function tensor_to_pil

benchmark/models/loger/utils/basic.py:89–105  ·  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

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

Callers

nothing calls this directly

Calls 1

array_to_pilFunction · 0.85

Tested by

no test coverage detected