vision icon indicating copy to clipboard operation
vision copied to clipboard

Allow `ToImage` to handle image paths

Open mantasu opened this issue 4 months ago • 2 comments

🚀 The feature

Was just wondering if it was possible to add support for handling image paths when using ToImage. Here's the desired feature:

import numpy as np
from PIL import Image
from torchvision.transforms.v2 import ToImage

img_np = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
Image.fromarray(img_np).save("test_img.jpg")

print(ToImage()("test_img.jpg").shape) # torch.Size([3, 100, 100])

Motivation, pitch

This simply allows us to avoid manually loading images, for example in custom Datasets:

class MyDataset(torch.utils.data.Dataset):
    def __init__(self):
        self.transform = v2.Compose([v2.ToImage(), v2.Normalize([0.5]*3, [0.3]*3)])
    def __getitem__(self, idx):
        return self.transform(f"path/to/img-{idx}.jpg")

Also, would be incredibly handful when using a standalone to_image utility.

Alternatives

No response

Additional context

No response

mantasu avatar Feb 07 '24 11:02 mantasu