-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Description
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:
keras/keras/src/utils/image_utils.py
Lines 171 to 173 in fa9c4ba
file_format: Optional file format override. If omitted, the format to | |
use is determined from the filename extension. If a file object was | |
used instead of a filename, this parameter should always be used. |
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"
:
keras/keras/src/utils/image_utils.py
Lines 179 to 183 in fa9c4ba
if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"): | |
warnings.warn( | |
"The JPG format does not support RGBA images, converting to RGB." | |
) | |
img = img.convert("RGB") |
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!