user9924779
user9924779

Reputation:

Why do I get 'numpy.ndarray' object has no attribute 'convert'?

I currently working on a machine learning project, and I implemented data augmentation by myself for some reason.

The type of image is

'PIL.JpegImagePlugin.JpegImageFile'

And I'd like to append my image to list called images.

#method "scale_augmentation" is self defined function. Return image.
f_processed = self.scale_augmentation(image = f) 

#error occurs here                
self.images.append(Image.fromarray(f_processed.convert('RGB'), dtype=np.float32) / 255.)

I'm stuck with this error for so long and I've gone through many stack overflow questions regarding to this error. Thanks in advance.


Scale_augmentation function looks like this

def scale_augmentation(image, scale_range=(256, 400), crop_size=224):
scale_size = np.random.randint(*scale_range)
image = imresize(image, (scale_size, scale_size))
image = random_crop(image, (crop_size, crop_size))
return image

Also error looks like this

--> 100 self.images.append(Image.fromarray(f_processed.convert('RGB'), dtype=np.float32) / 255.)
AttributeError: 'numpy.ndarray' object has no attribute 'convert'

Upvotes: 2

Views: 17167

Answers (1)

murat yalçın
murat yalçın

Reputation: 749

First convert it to image object by Image.fromarray then use convert

Try this:

self.images.append((Image.fromarray(f_processed, dtype=np.float32).convert('RGB') / 255.)

Clear steps are:

image = Image.fromarray(image)

image = image.convert("RGB")

now image is in jpeg format and you do:

image.save('test_test.jpeg')

Upvotes: 3

Related Questions