File size: 524 Bytes
413d4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import torch
from torchvision import transforms
from PIL import Image

def images_to_tensor(image_list):
    """
    Parse a list of PIL images and convert them to a PyTorch tensor in shape (T, C, H, W).
    """
    transform = transforms.ToTensor()
    
    # Convert each PIL image to tensor and store in a list
    tensor_list = [transform(img) for img in image_list]
    
    # Stack the list of tensors along a new dimension to create the final tensor
    tensor = torch.stack(tensor_list, dim=0)
    
    return tensor