When processing images, I encountered strange behavior when converting a PIL gray image to numpy array.
Here is a small code that loads a gray image image.
from keras.utils import img_to_array, array_to_img
a = keras.utils.load_img(img_src + "/00000.jpg")
b = img_to_array(a)
c = array_to_img(b) d = img_to_array(c)
After uploading the image in PIL format, it is displayed normally. The subsequent conversion of it to a numpy array and back changes the colors.
This screenshot shows the contents of the arrays
I’ve tried different image loading modes (RGB,greyscale), dtype type conversion – nothing helps.
I want to be able to save the image after conversion in its original form.
Original image
After conversion
vrs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.