Error message only when run locally"Please open the URL for reading and pass the result to Pillow"

Summary

I have an app with a matplotlib chart which has some images in it, these come from github URL, when running the deployed app on the cloud the code and images run and display correctly. When running the same code locally via conda I get the following error- “ValueError: Please open the URL for reading and pass the result to Pillow, e.g. with np.array(PIL.Image.open(urllib.request.urlopen(url))).
Traceback:”

This is the problem line

variation_icon = plt.imread(“https://github.com/nhsengland/making-data-count/blob/main/Icons/VariationIconImprovementLow.png?raw=true”)

  • Streamlit version: 1.23.1
  • Python version: 3.10.11
  • Using Conda
  • OS version: windows 10
  • Browser version: edge and chrome

Requirements file

pandas==1.2.4
numpy>1.20.1
streamlit> 1.11.1
matplotlib==3.4.3
seaborn== 0.11.2
typing-extensions>1.10.0.0

Links

Additional information

If needed, add any other context about the problem here.

1 Like

Thanks for reporting your issue, @mattchapman001!

Could you please try this and let me know if it would work?

import urllib.request
from PIL import Image
import numpy as np

url = "https://github.com/nhsengland/making-data-count/blob/main/Icons/VariationIconImprovementLow.png?raw=true"
with urllib.request.urlopen(url) as url_obj:
    variation_icon = np.array(Image.open(url_obj))

Thanks,
Charly

The error you are getting is because Streamlit is trying to load the image from the GitHub URL using the plt.imread() function. However, the plt.imread() function only supports loading images from local files. To load images from a URL, you need to use the urllib.request.urlopen() function to open the URL and then pass the result to the PIL.Image.open() function.

import streamlit as st
import urllib.request
from PIL import Image

def load_image_from_github(url):
  with urllib.request.urlopen(url) as response:
    image = Image.open(response)
  return image

image = load_image_from_github("https://github.com/nhsengland/making-data-count/blob/main/Icons/VariationIconImprovementLow.png?raw=true")

st.image(image)

To resolve this issue, you can modify your code to download the image from the URL and then open it with Pillow’s Image.open() function.

Try this and let me know If the issue has been fixed or not

Thanks for your help, issue resolved!

1 Like

Glad it worked!

Happy Streamlit-ing! :balloon:

Charly

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.