Skip to content

Allow ToImage to handle image paths #8261

Closed
@mantasu

Description

@mantasu

🚀 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions