Forum Archive

How to convert an UI image to a Pillow image

halloleooo

I am still experimenting with Pillow and UI images...

Now I try to convert a small UI image into a Pillow image, but somehow this doesn't work. First I tried:

ui_image = appex.get_images(image_type='ui')
pil_image = ui_image.to_png()
print(pil_image.size)

This gives my an error because ui_image.to_png() doesn't seem to create a Image (but only the data).

Then I thought about

...
s = ui_image.size
pil_image = Image.frombuffer(mode, size, ui_image.to_png())

but here I have no idea what to use for mode...

Any idea how to convert an UI image to a Pillow image?

cvp

@halloleooo
import io . . . def ui2pil(ui_img): return Image.open(io.BytesIO(ui_img.to_png()))

halloleooo

@cvp This is amazing! Where do you know all these tricks from? ๐Ÿ˜„

cvp

@halloleooo said:

Where do you know all these tricks from?

I'm sure this comes from the forum, full of amazing guys. This forum does not offer an easy way to search but if you use Google with "Pythonista forum pil ui.image" you should find it.

Of course, I have a lot of scripts thus I search among mines first.

ร‰dit: I'm sure that if you use a lot Pythonista, you'll become one of these amazing guys ๐Ÿ˜€

Robert_Tompkins

Possibly from here-ish?
https://forum.omz-software.com/topic/1935/how-can-i-convert-a-pil-image-to-a-ui-image/10

Iโ€™m going to try this out, I know very little about images/image-data. All Iโ€™m trying to do is compare two images, change pixel color for differences and draw a circle (Path.oval) around the bounds with some offset. Apparently I canโ€™t open an image and draw on it via ImageContext.. so.. conversion, conversion, conversion, undo all conversions, .show(). Since paste() appears to be broken as well.

Robert_Tompkins

@Robert_Tompkins

Traceback (most recent call last):
  File "/private/var/mobile/Containers/Shared/AppGroup/4696D4C5-8877-4DAB-9679-C1C82C4A5C18/Pythonista3/Documents/CompletedPrograms/ImageMatching.py", line 412, in <module>
    matchImage()
  File "/private/var/mobile/Containers/Shared/AppGroup/4696D4C5-8877-4DAB-9679-C1C82C4A5C18/Pythonista3/Documents/CompletedPrograms/ImageMatching.py", line 278, in matchImage
    ImageChops.screen(image1, outlinedDiff).show()
  File "/var/containers/Bundle/Application/24BCA42A-E70C-42BD-BD39-68CECBB90922/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages/PIL/ImageChops.py", line 138, in screen
    return image1._new(image1.im.chop_screen(image2.im))
ValueError: images do not match

Oops, one more conversion I guess ๐Ÿ˜