Reputation: 23
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
Upvotes: 0
Views: 112
Reputation: 161
In Pythonista, images in buttons are used by default as "templates", to be filled with the button tint color.
To use the actual image, you need to change the image rendering mode, as in this example:
import ui
view = ui.View()
image_as_template = ui.Image("test:Pythonista")
image_as_original = image_as_template.with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
button = ui.Button(image=image_as_original)
view.add_subview(button)
view.present()
Upvotes: 0