omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello โ€” I'm @olemoritz on Twitter.


    How to convert an UI image to a Pillow image

    Pythonista
    3
    6
    1821
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • halloleooo
      halloleooo last edited by 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 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @halloleooo last edited by cvp

        @halloleooo

        import io 
        .
        .
        .
        def ui2pil(ui_img):
          return Image.open(io.BytesIO(ui_img.to_png()))
        
        1 Reply Last reply Reply Quote 0
        • halloleooo
          halloleooo last edited by

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

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @halloleooo last edited by 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 ๐Ÿ˜€

            1 Reply Last reply Reply Quote 1
            • Robert_Tompkins
              Robert_Tompkins last edited by

              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 1 Reply Last reply Reply Quote 0
              • Robert_Tompkins
                Robert_Tompkins @Robert_Tompkins last edited by

                @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 ๐Ÿ˜…

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors