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

Remote images are lost when saving an Altair chart #59

Closed
binste opened this issue May 18, 2023 · 4 comments · Fixed by #60
Closed

Remote images are lost when saving an Altair chart #59

binste opened this issue May 18, 2023 · 4 comments · Fixed by #60
Labels
bug Something isn't working

Comments

@binste
Copy link

binste commented May 18, 2023

When saving an Altair chart which references remote images, those images are lost. It works if I first save the images locally and reference them on the file system. Is there a way how vl-convert could fetch those images and include them? I think this would in general be nice for image marks, but my specific use case is that I'm working on adding support for basemaps in Altair/Vega-Lite. The tiles are loaded from an arbitrary XYZ server using the same approach as in the code example below. See this VL PR for a full VL spec and a screenshot of what it looks like. (I might first implement this functionality in an altair extension package before it hopefully lands in VL)

Code example

import altair as alt
import pandas as pd

source = pd.DataFrame.from_records(
    [
        {
            "x": 0.5,
            "y": 0.5,
            "img": "https://vega.github.io/vega-datasets/data/ffox.png",
        },
        {
            "x": 1.5,
            "y": 1.5,
            "img": "https://vega.github.io/vega-datasets/data/gimp.png",
        },
        {
            "x": 2.5,
            "y": 2.5,
            "img": "https://vega.github.io/vega-datasets/data/7zip.png",
        },
    ]
)

image_chart = alt.Chart(source).mark_image(width=50, height=50).encode(x="x", y="y", url="img")
image_chart

image

Saving this chart gives me the following:

image_chart.save("image_chart.png")

image

@jonmmease jonmmease added the bug Something isn't working label May 18, 2023
@jonmmease
Copy link
Collaborator

Thanks for the report @binste. Are the remote images present if you save to SVG?

I'm guessing resvg handles loading image references when they are local but doesn't pull them from remote sources. So we'll probably need to download them to a temp directory at some point in the pipeline (not sure if it would be easier to do this at the Vega and Vega-Lite spec level or at the SVG level).

@binste
Copy link
Author

binste commented May 18, 2023

They are present if I save as SVG and they are embedded as e.g. <image aria-label="x: 0.5; y: 0.5; img: https://vega.github.io/vega-datasets/data/ffox.png" role="graphics-symbol" aria-roledescription="image mark" xlink:href="https://vega.github.io/vega-datasets/data/ffox.png" ....

Yeah the temp directory solution sounds reasonable. I'm not familiar with resvg so don't know if it has any capabilities to override how it converts external images? Or as you said, the images could be replaced in the Vega/VL specs or maybe in the SVG string itself but I guess that would require some SVG manipulation library.

@jonmmease
Copy link
Collaborator

resvg relies on usvg for the initial SVG processing, and it looks like it's possible to override the image url resolver: https://docs.rs/usvg/latest/usvg/struct.ImageHrefResolver.html#

This type can be useful if you want to have an alternative xlink:href handling to the default one. For example, you can forbid access to local files (which is allowed by default) or add support for resolving actual URLs (usvg doesn’t do any network requests).

@jonmmease
Copy link
Collaborator

Released in version 0.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants