-1

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

2
  • Please provide a minimal complete reproducible executable example
    – Caridorc
    Nov 19 at 20:37
  • you have try without except. Better create minimal working code - so we could simply copy and test it. You could also show your images. Maybe problem is in images.
    – furas
    Nov 20 at 1:26

1 Answer 1

0

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()
1
  • Yes, that worked. Thank you :)
    – nasun
    Nov 21 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.