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

Process and convert .svg files in Image #6500

Merged
merged 12 commits into from Nov 21, 2023
Merged

Process and convert .svg files in Image #6500

merged 12 commits into from Nov 21, 2023

Conversation

hannahblair
Copy link
Collaborator

@hannahblair hannahblair commented Nov 20, 2023

Description

We were using PIL to process images in gr.Image() but PIL doesn't support svg files. I've added a lib called cairosvg to allow us to process svg files and it converts the file to png in the preprocess method.

Closes: #6489

import gradio as gr


def identity(image):
    return image

demo = gr.Interface(
    fn=identity,
    inputs=gr.Image(),
    outputs=gr.Image(show_download_button=True),
)

demo.launch()

🎯 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 20, 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/b8d34e0af2b3987bc232adebed82b8ab4158a6ef/gradio-4.5.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Nov 20, 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.

Process and convert .svg files in Image

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.

@hannahblair hannahblair marked this pull request as draft November 20, 2023 14:15
@abidlabs
Copy link
Member

Thanks @hannahblair for taking this on. We've been very careful about adding more Python dependencies (actually trying to cut down on our existing ones). This use case is a little too niche to add the cairosvg package as a dependency imo. We could instead:

  1. Vendor in the necessary code from the cairosvg into the gradio library (so that we get this functionality without adding an additional dependency)

  2. Or if not possible, we change the frontend not to except SVG files either and document the Image component accordingly. We could then publish this as a separate SVGImage componen

@hannahblair
Copy link
Collaborator Author

@abidlabs! Ok good to know! I wasn't 100% if this was the right way to go. I'll consider both options and make the changes!

@whitphx
Copy link
Member

whitphx commented Nov 21, 2023

There are some ML models whose I/O format is SVG, not raster images, such as arXiv:2306.06094, so allowing passing SVG files directly would be an option which helps such use cases.

@hannahblair
Copy link
Collaborator Author

hannahblair commented Nov 21, 2023

There are some ML models whose I/O format is SVG, not raster images, such as arXiv:2306.06094, so allowing passing SVG files directly would be an option which helps such use cases.

In light of this, I've removed the svg to png conversion and tweaked the logic just to return the SVG file path. You can upload an SVG and it will retain that format in the output. WDYT?

@hannahblair hannahblair marked this pull request as ready for review November 21, 2023 15:17
@abidlabs
Copy link
Member

Works great! Thanks so much @hannahblair

hannahblair and others added 2 commits November 22, 2023 00:27
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
@hannahblair hannahblair merged commit 830b6c0 into main Nov 21, 2023
21 checks passed
@hannahblair hannahblair deleted the image-process-svg branch November 21, 2023 23:50
@pngwn pngwn mentioned this pull request Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gr.Image() accepts an SVG file as an input on the frontend, but it emits an error after submission.
5 participants