Reading Gray Image: Numpy array : transforms.ToPILImage(mode='L') object() takes no parameters

I am reading a single image

    import torchvision.transforms as transforms    
    img = misc.imread(path)
    #<type 'numpy.ndarray'>
    #shape (256, 256)
    #then I change shape to (256,256,1)
    img = img[:,:,np.newaxis]
    toPIL = transforms.ToPILImage(mode='L')
    img = toPIL(img)

Getting this complain:

toPIL = transforms.ToPILImage(mode='L')
TypeError: object() takes no parameters

Not sure how to set mode in transforms.ToPILImage. if there is no way to manually set this, how should I read 1 channel image?

It’s working fine in my system. Which version of pytorch are you using.

Are you using torchvision-0.1.9? It used to determine mode from the numpy type. Snippet from my installation (

if npimg.dtype == np.uint8:
    mode = 'L'
if npimg.dtype == np.int16:
    mode = 'I;16'
if npimg.dtype == np.int32:
    mode = 'I'
elif npimg.dtype == np.float32:
    mode = 'F'