Now… Change the image in a UI...
Using the same method recommended to change text in a label (previous post), I am now trying to change an image based on a photo taken by the camera. The program tries, but returns a blank image to the UI. The image is definitely being taken, because I can display it to the console - just not in the ImageView image on the UI.
Here's the code:
…myimage = myimage.resize((50,50),Image.BILINEAR)
The issue here is that there is no built-in way to convert a PIL image (as returned by
photos) to a
ui.Image(some_pil_image)doesn't work. Here's how I managed to do the conversion, with the help of the
import StringIO import ui def pil_to_ui_image(img): strio = StringIO.StringIO() img.save(strio, img.format) data = strio.getvalue() strio.close() return ui.Image.from_data(data)
pil_to_ui_image()function with a PIL image as an argument will return an identical
Figured it out. Needed to modify the last line in the def to:
Fine, I suppose it also works that way..? I did not know that was possible. The documentation on that function says that it expects raw image data, not that it can also take a PIL image object.
Oops, yes, you're correct. I had added this line in the def after posting the first time:
myimage = photos.get____image(raw____data=True)
But thanks for your response… I will eventually need to know how to make a PIL conversion too!