Forum Archive

Simple Imageview help

donnieh

I want to try to get the following example working. The goal is to have a picture be a sub-view of a scroll view. Why doesn't the following example work?

​ import ui import Image sv = ui.ScrollView() sv.background_color = 'gray' im = Image.open('Test_Boat') iv = ui.ImageView() iv.image = im sv.add_subview(iv) sv.present()

Phuket2

If you change your Image.open
to
im = ui.Image.named('Test_Boat')
It should work

Webmaster4o

ui uses a different image class than PIL. To convert PIL images to UI, I typically use:

def pil_to_ui(img):
    b = BytesIO()
    img.save(b, "PNG")
    data = b.getvalue()
    b.close()
    return ui.Image.from_data(data)

That function takes a PIL image as input, and returns a ui.Image as output.

donnieh

Thanks Phuket2. It worked well. I did not see ui.Image.named()in the Pythonista documentation so I did not know that existed.

Phuket2

@donnieh ,
Actually named.image is in the documentation, ui.Image class. In this case I think it's more about examples. I had a hard time with this also before

JonB

Part of the problem is that the ImageView docs state that .image should be an Image rather than ui.Image. The hyperlink takes you directly to the PIL Image docs. Sort of a documentation bug :)

cvp

@omz doc incoherence still there😢

zrzka

@cvp did report as https://github.com/omz/Pythonista-Issues/issues/516. Please, try to file issues for these things, it's really hard and time consuming to keep track of all these discussion. Thanks.

cvp

@zrzka Ok, sorry😢