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.


    Now… Change the image in a UI...

    Pythonista
    3
    7
    4752
    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.
    • nasadave
      nasadave last edited by

      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:

      v=ui.load_view('MyUI')

      v.present('sheet')

      @ui.in_background

      def getimage():

      …myimage=photos.capture_image()

      …imageview1=v['imageview1']

      …myimage = myimage.resize((50,50),Image.BILINEAR)

      …imageview.image=ui.Image(myImage)

      getimage()

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        The issue here is that there is no built-in way to convert a PIL image (as returned by photos) to a ui.Image; doing ui.Image(some_pil_image) doesn't work. Here's how I managed to do the conversion, with the help of the StringIO module:

        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)
        

        Calling the pil_to_ui_image() function with a PIL image as an argument will return an identical ui.Image.

        1 Reply Last reply Reply Quote 0
        • nasadave
          nasadave last edited by

          Figured it out. Needed to modify the last line in the def to:

          imageview1.image=ui.Image.from_data(myimage)

          That worked.

          1 Reply Last reply Reply Quote 0
          • dgelessus
            dgelessus last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • nasadave
              nasadave last edited by

              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)

              1 Reply Last reply Reply Quote 0
              • nasadave
                nasadave last edited by

                But thanks for your response… I will eventually need to know how to make a PIL conversion too!

                1 Reply Last reply Reply Quote 0
                • ?
                  A Former User last edited by

                  @nasadave Or like this... http://omz-forums.appspot.com/pythonista/post/5232661803040768

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