Simple Imageview help
-
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()```
-
If you change your Image.open
to
im = ui.Image.named('Test_Boat')
It should work
-
ui
uses a different image class thanPIL
. 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.
-
Thanks Phuket2. It worked well. I did not see
ui.Image.named()
in the Pythonista documentation so I did not know that existed.
-
@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
-
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 :)
-
@omz doc incoherence still there😢
-
@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.
-
@zrzka Ok, sorry😢