3

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'
5
  • 2
    Because a numpy.ndarray is not an Image. you probably want to call convert on the Image object. Commented Oct 12, 2018 at 15:19
  • @Graipher Thanks for your reply! I'm new to ml, sorry for my beginner question. I though f_processed is Image, so I call convert('RGB') on Image, not ndarray. How can I fix it? Commented Oct 12, 2018 at 15:25
  • You should check that is true. Some operation might return an array instead of the image object. Just check type(f_processed). Commented Oct 12, 2018 at 15:26
  • @Graipher type was <class 'PIL.Image.Image'>. I also added scale_augmentation function on question form. Could you look at it? Commented Oct 12, 2018 at 15:36
  • You'll need to add one or more type checks on f_processed or image till you find where you start to get ndarray instead of the expected Image. Since you haven't provided full working code, only you can add those debugging tests. Commented Oct 12, 2018 at 16:39

1 Answer 1

4

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')

Sign up to request clarification or add additional context in comments.

1 Comment

what is Image?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.