Forum Archive

Presenting Image.Image in a ui.ImageView

Omega0

I want to display an image in a ui from the camera roll, but I get the error: Expected a ui.Image. How do I convert an image from Image.Image to ui.Image?

Here's what I have so far:

# coding: utf-8

import tempfile, photos, ui

@ui.in_background
def get_image(sender):
    view = sender.superview
    image = view['Image']
    try:
        new = photos.pick_image()
        new.show()
        new = new.tostring()
        image.image = ui.Image.from_data(new)
        image.image.show()
    except:
        view.close()
        raise

view = ui.View(background_color = '#FFFFFF')
view.add_subview(ui.Button(name = 'image button', frame = (0,0,80,32)))
view['image button'].action = get_image
view['image button'].title = 'Set image'
view.add_subview(ui.ImageView(name = 'Image', frame = (0,32,80,100)))
view.present()
ccc

Replace the 5 lines between try and except with these two lines:

        raw_photo = photos.pick_image(raw_data=True)
        image.image = ui.Image.from_data(raw_photo)
Omega0

Perfect, that's exactly what I was looking for, although I ended up implementing it as a one liner:

image.image = ui.Image.from_data(photos.pick_image(raw_data = True))