Files
2025-08-06 00:20:49 +08:00

18 lines
363 B
Python

import torch
from PIL import Image
import numpy as np
# Tensor to PIL
@staticmethod
def tensor2pil(image):
return Image.fromarray(
np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
)
# PIL to Tensor
@staticmethod
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)