Reputation: 437
I always use this parameter to scale array of original image pixel values to be between [0,1] and specify the parameter rescale=1./255
.
Then when i do this:
from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
img = load_img('val_00009301.JPEG')
img_arr = img_to_array(img)
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'):......`
When I check "path/to/save" directory, I see the picture generated by ImageDataGenerator class totally normal. How that is happen? I should see almost completely black image.
Upvotes: 8
Views: 28425
Reputation: 2522
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]
Upvotes: 6
Reputation: 2621
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.
Upvotes: 4