I am trying to load images from an url into a button, but they all appear blue.
def load_jpeg_image(image_url):
# Fetch image data from the URL
image_data = requests.get(image_url).content
try:
# Create a PIL Image from the JPEG data
pil_image = Image.open(io.BytesIO(image_data))
# Convert to RGB color space
pil_image_rgb = pil_image.convert('RGB')
# Save the RGB image to a BytesIO object
rgb_image_data = io.BytesIO()
pil_image_rgb.save(rgb_image_data, format='JPEG')
rgb_image_data = rgb_image_data.getvalue()
# Create a UI Image from the RGB data
ui_image = ui.Image.from_data(rgb_image_data)
return ui_image
# Create a view
view = ui.View()
view.background_color = 'white' # Set the background color to white
# Set up variables for layout
margin = 10
image_width = (view.width - 4 * margin) / 3
image_height = image_width
for i, image_url in enumerate( image_urls):
# Create a button with the image
image_button = ui.Button(frame=(margin + i % 3 * (image_width + margin), margin + i // 3 * (image_height + margin), image_width, image_height))
image_button.name = str(i)
image_button.image = load_jpeg_image(image_url)
image_button.action = image_tapped
view.add_subview(image_button)
When looking at the link the images are there and not blue. I try to load jpg.
Thanks
try
withoutexcept
. Better createminimal working code
- so we could simply copy and test it. You could also show your images. Maybe problem is in images.