Home > database >  Converting and manipulation tf data image dataset straight from a folder
Converting and manipulation tf data image dataset straight from a folder

Time:06-22

I am trying to load a dataset from a local folder and use it as a tf data dataset. The folder structure is :

   ../dataset/
      class_0/
      class_1/

where class 0 sub-fodler contains all images with class 0 and class 1 all with class 1.
To achieve this my code is :

images = image_dataset_from_directory('../dataset/',
                                             shuffle=True,
                                             batch_size=32,
                                             image_size=(1080,1920))

all images are of size (1080,1920,3) or (1920,1080,3)

I am trying to show an image using:

for image, labels in images.take(1):

      img = image[0].numpy() # take first image of batch
      print(img.shape)
      img = Image.fromarray(img, 'RGB')
      img.save('my.png')
      img.show()

which prints image shape= (1080, 1920, 3)

However the image showed by PIL is distrorted and seems like random noise.

Any idea about what i am doing wrong?

CodePudding user response:

The problem seems to be with floating point numbers for Pillow

In your converting function, you have img = Image.fromarray(img, 'RGB').

Changing this to img = Image.fromarray(img.astype('uint8'), 'RGB') should solve this issue.

  • Related