Bug Issue

I found an issue on keras.utils.save_img() with arg file_format='jpg', the doc of keras.utils.save_img() shows its description as below:

https://github.com/keras-team/keras/blob/fa9c4ba633ca184499650f210d654485cd65e7b1/keras/src/utils/image_utils.py#L171-L173

See the repro below, with TensorFlow 2.19.0 and Keras nightly:

Repro

import numpy as np
import keras

file_format = 'jpg' # jpeg, png is OK, jpg will raise error

image_array = np.random.randint(0, 256, size=(100, 100, 3), dtype=np.uint8)
image_array = (((image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array))) * 255)
keras.utils.save_img(('random_image.' + file_format), image_array, data_format='channels_last', file_format=file_format)

Output

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[12], line 8
      6 image_array = np.random.randint(0, 256, size=(100, 100, 3), dtype=np.uint8)
      7 image_array = (((image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array))) * 255)
----> 8 keras.utils.save_img(('random_image.' + file_format), image_array, data_format='channels_last', file_format=file_format)

File ~/anaconda3/envs/testoracle-tf/lib/python3.10/site-packages/keras/src/utils/image_utils.py:184, in save_img(path, x, data_format, file_format, scale, **kwargs)
    180     warnings.warn(
    181         "The JPG format does not support RGBA images, converting to RGB."
    182     )
    183     img = img.convert("RGB")
--> 184 img.save(path, format=file_format, **kwargs)

File ~/anaconda3/envs/testoracle-tf/lib/python3.10/site-packages/PIL/Image.py:2581, in Image.save(self, fp, format, **params)
   2579     save_handler = SAVE_ALL[format.upper()]
   2580 else:
-> 2581     save_handler = SAVE[format.upper()]
   2583 created = False
   2584 if open_fp:

KeyError: 'JPG'

Firstly, I think that maybe this func is not supported for 'jpg', but see the codes below,

It deals with the situation when img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"):, which refers that file_format == "jpg" is accepted when img.mode != "RGBA":

https://github.com/keras-team/keras/blob/fa9c4ba633ca184499650f210d654485cd65e7b1/keras/src/utils/image_utils.py#L179-L183

So I try the other types, the results show that file_format == 'jpeg' or file_format == 'png' will run well without Exception, only 'jpg' will raise error.

I wonder why this happened yet, not sure~.

Thanks for noting!

Comment From: sonali-kumari1

Hi @ILCSFNO - Thanks for reporting this issue. I have tested your code with the latest version of Keras 3.11.3 and encountered the same KeyError. The error originates from the Pillow library, when calling Image.save(). It appears that Pillow's Image.save() function does not recognize 'JPG' as a valid format.

While Keras explicitly checks for jpg format in this line:

if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"):

it internally uses PIL library for saving the image which leads to the error. As a workaround, please use jpeg or png formats for the file_format argument. You can refer to this documentation for more information about supported formats by Pillow. Thanks!

Comment From: ILCSFNO

Alright! I'll fix the check together with the docstring then.

Comment From: georgeneedles60-hub

Projects (1).csv #21693