ToTensor()(image) arguments

Hi:)

I am trying to iterate over pictures from folder, using PIL.Image to open and resize them, and then torchvision.transforms.ToTensor() to save them as tensors.

On the ToTensor() command I get this error:
TypeError: array() takes 1 positional argument but 2 were given

# read from given dir, resize to imageSizeXimageSize
def readImages(dir, imageSize): 
    # process one given raw image
    def processImg(rawImg):
        image = Image.open(rawImg)
        image.thumbnail((imageSize, imageSize), Image.ANTIALIAS)
        image = image.resize((imageSize, imageSize), Image.ANTIALIAS)
        image = torchvision.transforms.ToTensor()(image)
        return image

    images = []
    for rawImg in (Path(dir).glob('*.png')):
        images.append(processImg(rawImg))
    return images

someone help?

This looks like an issue that has recently surfaced; you might want to see if downgrading to PIL 8.2 works.
Issues:
transform toTensor fails with PIL image · Issue #4146 · pytorch/vision (github.com)
Pillow 8.3 and NumPy · Issue #5571 · python-pillow/Pillow (github.com)

2 Likes

It works thanks very much!