Hi PyTorch buddies,
I am reading in Nifti images using nibabel and try to do some preprocessing on the image using torchvision.transforms. However, I realised that it has to be converted to PIL Image before I can use functions in torchvision.transforms.
What I achieved was that I loaded the image and converted it to a numpy array of data type of float32. However, I could not convert the array to PIL Image with the error of “Cannot handle this data type”. I looked for some solutions online by changing the data type to “uint8”, but in this case I shouldn’t change my data type to that. I also tried different conversion of data types, but they also didn’t work out.
Below are the codes that I wrote:
image = nib.load(dir_img)
image_data = image.get_fdata().copy()  
imgData_ft = image_data.astype(np.float32)
imgData_ft = np.squeeze(imgData_ft)
imgData_tensor = transform(imgData_ft)

where transform is defined as follows:
transform = transforms.Compose([    
    transforms.ToPILImage(),
    transforms.CenterCrop(159),
    transforms.ToTensor()
    ])

I am glad to hear any clue from you! Thank you so much for your help.