How does Keras ImageDataGenerator rescale parameter works?

I altered your example a little to plot the image and to print a pixel value. It seems that the image is automagically rescaled back when plotted, because I did not noticed any difference between my input image and the plotted one. I assume the same happens when saving.

from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
import numpy as np
from matplotlib import pyplot

img = load_img('capture102.jpg')
img_arr = np.expand_dims(img_to_array(img), axis=0)
datagen = ImageDataGenerator(rescale=1./255)

for batch in datagen.flow(img_arr, batch_size=1, save_to_dir='path/to/save', save_prefix='1_param', save_format='jpeg'):
    print(batch[0][0][0])
    pyplot.imshow(batch[0])
    pyplot.show()
    break

The printed values are:[0.21960786 0.23529413 0.27058825]


This is because when you save it to disk, array_to_img() function rescale it back to the image range, i.e. 0-255 for uint8. See the keras image data generator implementation for details.

Tags:

Python

Keras