question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using locally saved PNGs in mark_image

See original GitHub issue

Hello,

I’m trying to use mark_image for png’s stored locally, and I’m running into some issues.

First, if I use a remote location (using dropbox), it works fine:

url = "https://dl.dropboxusercontent.com/s/5jxbltadssmjbbwngk2/EfX_transparent.png"
df = pd.DataFrame(dict(x=[1], y=[1], url=[url]))
alt.Chart(df).mark_image().encode(x='x', y='y', url='url')

image

I’d rather not have to upload an image to a remote location for altair to reference it. I feel like I must be doing something wrong. Switching out the URL for a local location like url=r’my_folder/EfX_transparent.png’ doesn’t work. The chart shows up blank.

Though, it looks like something like this should work, as seen here:

image

So, what’s the search path for altair? Any ideas would be appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
githubbarcommented, Mar 1, 2021

You could also paste the the actual image as a base64 encoded string by opening an image inside JupyterLab, right clicking, and selecting “Copy image address”. The string will look something like this data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAACsCAY...

Thank you! This is what helped to export embedded images in HTML. You can also automate it in code:

import altair as alt
import pandas as pd
from IPython.display import Image
import base64, io, IPython
from PIL import Image as PILImage

images = ["./img00.jpg", "./img01.jpg", "./img02.jpg"]
imgCode = []


for imgPath in images:
    image = PILImage.open(imgPath)
    output = io.BytesIO()    
    image.save(output, format='JPEG')
    encoded_string = "data:image/jpeg;base64,"+base64.b64encode(output.getvalue()).decode()
    imgCode.append(encoded_string)

x = [0.5, 1.5, 2.5]
y = [0.5, 1.5, 2.5]
source = pd.DataFrame({"x": x, "y": y, "img": imgCode})
alt.Chart(source).mark_image(
    width=50,
    height=50
).encode(
    x='x',
    y='y',
    url='img'
)
2reactions
joelostblomcommented, Jan 19, 2022

If anyone is looking for how to do this in JupyterLab, you need to find the image in the file browser, right click it, select “Copy download link”, and (optionally) delete any letters after your image file name so that it looks something like this: http://localhost:8888/files/your-image.png. It also seems to work with just /files/your-image.png.

You could also paste the the actual image as a base64 encoded string by opening an image inside JupyterLab, right clicking, and selecting “Copy image address”. The string will look something like this data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAACsCAY...

Edit: related later SO post https://stackoverflow.com/questions/70751037/altair-tooltip-with-local-pil-image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I display local image in markdown? - Stack Overflow
Try to put the image in the same folder as your Notebook and use: ![alt text](Isolated.png "Title"). On windows the desktop should be...
Read more >
Loading and Saving Images in Python | by Renu Khandelwal
Simple ways to load and save images using PIL, OpenCV, and Matplotib ... You can save the JPEG image in a different format...
Read more >
Writing/Saving an Image - The Java Tutorials
This lesson started with an explanation for using the javax.imageio package, to load images from an external image format into the internal BufferedImage...
Read more >
TinyPNG – Compress WebP, PNG and JPEG images ...
You can use Save for Web to export your images as 24-bit transparent PNG files and upload them to TinyPNG. We'll convert them...
Read more >
Static image export in Python - Plotly
Save the image to your local computer, or embed it inside your Jupyter ... You can export figures either to static image file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found