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
5 changes: 5 additions & 0 deletions .changeset/pretty-ads-do.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Process and convert .svg files in `Image`
10 changes: 9 additions & 1 deletion gradio/components/image.py
Expand Up @@ -147,6 +147,7 @@ def preprocess(
) -> np.ndarray | _Image.Image | str | None:
if payload is None:
return payload
file_path = Path(payload.path)
if payload.orig_name:
p = Path(payload.orig_name)
name = p.stem
Expand All @@ -156,7 +157,11 @@ def preprocess(
else:
name = "image"
suffix = "png"
im = _Image.open(payload.path)

if suffix.lower() == "svg":
hannahblair marked this conversation as resolved.
Show resolved Hide resolved
return str(file_path)

im = _Image.open(file_path)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
im = im.convert(self.image_mode)
Expand All @@ -173,6 +178,9 @@ def postprocess(
) -> FileData | None:
if value is None:
return None

if isinstance(value, str) and value.lower().endswith(".svg"):
return FileData(path=value, orig_name=Path(value).name)
saved = image_utils.save_image(value, self.GRADIO_CACHE)
orig_name = Path(saved).name if Path(saved).exists() else None
return FileData(path=saved, orig_name=orig_name)
Expand Down
19 changes: 19 additions & 0 deletions js/app/test/files/gradio-logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions js/app/test/image_component_events.spec.ts
Expand Up @@ -26,6 +26,22 @@ test("Image click-to-upload uploads image successfuly. Clear button dispatches e
await page.getByLabel("Remove Image").click();
await expect(page.getByLabel("# Clear Events")).toHaveValue("1");
await expect(page.getByLabel("# Change Events").first()).toHaveValue("2");

await Promise.all([
uploader.setInputFiles(["./test/files/gradio-logo.svg"]),
page.waitForResponse("**/upload?*?*")
]);

await expect(page.getByLabel("# Change Events").first()).toHaveValue("3");
await expect(await page.getByLabel("# Upload Events")).toHaveValue("2");
await expect(await page.getByLabel("# Change Events Output")).toHaveValue(
"2"
);

const SVGdownloadPromise = page.waitForEvent("download");
await page.getByLabel("Download").click();
const SVGdownload = await SVGdownloadPromise;
expect(SVGdownload.suggestedFilename()).toBe("gradio-logo.svg");
});

test("Image drag-to-upload uploads image successfuly.", async ({ page }) => {
Expand Down