-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Fix: Support 'jpg' format in keras.utils.save_img() #21683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
bf4e0ce
b2a5668
d93f3c5
9ab0b4f
b652534
4a6a78e
524252e
97d00f5
2a24eaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import numpy as np | ||
import os | ||
from keras.utils import save_img | ||
|
||
def test_save_jpg_rgb(tmp_path): | ||
img = np.random.randint(0, 256, size=(50, 50, 3), dtype=np.uint8) | ||
path = tmp_path / "rgb.jpg" | ||
save_img(path, img, file_format="jpg") | ||
assert os.path.exists(path) | ||
|
||
def test_save_jpg_rgba(tmp_path): | ||
img = np.random.randint(0, 256, size=(50, 50, 4), dtype=np.uint8) | ||
path = tmp_path / "rgba.jpg" | ||
save_img(path, img, file_format="jpg") | ||
assert os.path.exists(path) | ||
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -175,8 +175,10 @@ def save_img(path, x, data_format=None, file_format=None, scale=True, **kwargs): | |||||||||||||||||||||||||||
**kwargs: Additional keyword arguments passed to `PIL.Image.save()`. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
data_format = backend.standardize_data_format(data_format) | ||||||||||||||||||||||||||||
if file_format is not None and file_format.lower() == 'jpg': | ||||||||||||||||||||||||||||
file_format = 'jpeg' | ||||||||||||||||||||||||||||
img = array_to_img(x, data_format=data_format, scale=scale) | ||||||||||||||||||||||||||||
if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"): | ||||||||||||||||||||||||||||
if img.mode == "RGBA" and file_format in ["jpeg", "jpg"]: | ||||||||||||||||||||||||||||
|
if file_format is not None and file_format.lower() == 'jpg': | |
file_format = 'jpeg' | |
img = array_to_img(x, data_format=data_format, scale=scale) | |
if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"): | |
if img.mode == "RGBA" and file_format in ["jpeg", "jpg"]: | |
_format = file_format | |
if _format is None and isinstance(path, (str, pathlib.Path)): | |
_format = pathlib.Path(path).suffix[1:].lower() | |
if file_format is not None and file_format.lower() == 'jpg': | |
file_format = 'jpeg' | |
img = array_to_img(x, data_format=data_format, scale=scale) | |
if img.mode == "RGBA" and _format in ("jpeg", "jpg"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are a good start, but they can be improved. The two test functions are very similar and can be combined into a single parameterized test using
pytest.mark.parametrize
for better maintainability. Also, the assertions only check for file existence. It would be more robust to load the saved image and verify its content, especially to confirm that RGBA images are correctly converted to RGB.