Skip to content
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

Preserve original image extension in backend processing #6456

Merged
merged 6 commits into from Nov 16, 2023

Conversation

freddyaboulton
Copy link
Collaborator

Description

Jpegs were being converted to png always.

e2e test reflects this change.

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Nov 16, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/3347301721498a4279402eae73d8f6ee2f3d9c1b/gradio-4.3.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@3347301721498a4279402eae73d8f6ee2f3d9c1b#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Nov 16, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Preserve original image extension in backend processing

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton freddyaboulton marked this pull request as ready for review November 16, 2023 16:08
@freddyaboulton freddyaboulton added the v: patch A change that requires a patch release label Nov 16, 2023
@@ -150,8 +150,12 @@ def preprocess(
if payload.orig_name:
p = Path(payload.orig_name)
name = p.stem
suffix = p.suffix.replace(".", "")
if suffix in ["jpg", "jpeg"]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird that PIL doesn't recognize "jpg" as a valid format

@@ -27,7 +28,7 @@ def format_image(
return np.array(im)
elif type == "filepath":
path = processing_utils.save_pil_to_cache(
im, cache_dir=cache_dir, name=name, format=fmt or "png" # type: ignore
im, cache_dir=cache_dir, name=name, format=fmt or format # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only risk (admittedly small) I can see here is that there are some image formats that PIL can read but not save as: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#read-only-formats

They all look very rare to me, but perhaps a more robust approach would be to try / catch this, and save as "png" as a fallback

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's a good point

@@ -21,7 +21,7 @@ test("Image click-to-upload uploads image successfuly. Clear button dispatches e
await page.getByLabel("Download").click();
const download = await downloadPromise;
// Automatically convert to png in the backend since PIL is very picky
await expect(download.suggestedFilename()).toBe("cheetah1.png");
await expect(download.suggestedFilename()).toBe("cheetah1.jpeg");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@abidlabs
Copy link
Member

Awesome thanks for taking care of this @freddyaboulton. Left a small note, otherwise lgtm

@@ -39,10 +40,12 @@ def format_image(


def save_image(y: np.ndarray | _Image.Image | str | Path, cache_dir: str):
# numpy and PIL get saved to png as default format
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to account for like 90% of cases? Does opening an image with PIL or numpy not retain any meta information about the file that we can use to recreate the format?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do better for numpy as there's no natural encoding choice. But we can try to read the format property for PIL.

@freddyaboulton
Copy link
Collaborator Author

Just pushed up suggested changes @pngwn and @abidlabs !

gradio/image_utils.py Outdated Show resolved Hide resolved
gradio/image_utils.py Outdated Show resolved Hide resolved
@abidlabs
Copy link
Member

abidlabs commented Nov 16, 2023

Nice thanks @freddyaboulton! I suggested a more specific exception, otherwise lgtm

freddyaboulton and others added 2 commits November 16, 2023 17:20
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
@freddyaboulton freddyaboulton merged commit 3953a14 into main Nov 16, 2023
15 checks passed
@freddyaboulton freddyaboulton deleted the preserve-file-extension-image-preprocessing branch November 16, 2023 22:47
@freddyaboulton
Copy link
Collaborator Author

Thanks for the reviews @abidlabs @pngwn !

@pngwn pngwn mentioned this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants