0

I am creating images using PIL based on arrays of size 20000 pixels wide and 1 pixel high, these pixels are coded with RGB values. However, once saved to my computer and processed using ImageDataGenerator, all RGB values are turned to 0. I do not understand why that is the case. When I use datagen.flow, the returned array or image is correct, but when using datagen.flow_from_directory, that is why I get a problem.

This is the code used to create the image:

final_arr=np.column_stack((hist,np.round(ratio*10),np.zeros(20000))).astype(np.uint8)
# Reshape the array to (n, 3) and convert to a tuple of tuples
rgb_tuples = tuple(map(tuple, final_arr))
               
# Create an image from the array of tuples
image = Image.new('RGB', (final_arr.shape[0],1))
image.putdata(rgb_tuples)
        
# Print the size of the image
print(f"Image size: {image.size}")      

print(hist)   
print(np.array(image))
        
image.save(f'{filename}_TESThistogram.png')

This gives:

[[[ 1  0  0]
  [ 2 10  0]
  [ 3  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]]

But then, I use:

train_datagen = ImageDataGenerator(
    preprocessing_function=False,
    rotation_range=None,
    width_shift_range=None,
    height_shift_range=None,
    horizontal_flip=False,
    # Set other parameters as needed
)
# validation_datagen = ImageDataGenerator(preprocessing_function=preprocess_image)
imgeas='/home/sam/sra_im/train1/cancer'
im=os.listdir(imgeas)
ima=os.path.join(imgeas, im[0])
ima=Image.open(ima)
ima=ima.convert('RGB')
ima=np.array(ima)
print(ima)
print(ima.shape)

#point the generator to the directory not the subdirectory, the names of the subdirectory are the image labels
train_generator = train_datagen.flow_from_directory(
    '/home/sam/sra_im/train1/',
    target_size=(20000, 1),
    batch_size=1,
    color_mode='rgb'
)
images, labels = train_generator.next()
print(train_generator.image_shape)
print(images)

and obtain this: *The array is from print(images)

[[[[0. 0. 0.]]

  [[0. 0. 0.]]

  [[0. 0. 0.]]

  ...

  [[0. 0. 0.]]

  [[0. 0. 0.]]

  [[0. 0. 0.]]]]
Alexburn
  • 1
  • 1
  • You obtain that where? Is that what's in `train_generator`? Or is that `ima`? – Tim Roberts Jul 13 '23 at 00:53
  • @TimRoberts This is what is printed from train_generator, I have edited the question to show that. ima was simply to allow me to print the expected image. – Alexburn Jul 13 '23 at 02:39
  • Do you have more than the one subdirectory? – Tim Roberts Jul 13 '23 at 03:05
  • @TimRoberts I have two subdirectories inside train1 which are called cancer and control, but for this particular example, I only included one image in the cancer subdirectory simply to see what was printed out with a single image. – Alexburn Jul 13 '23 at 03:59

0 Answers0