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:”
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))
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 stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.