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 load an image into image view?

    Pythonista
    ui.image ui.imageview
    4
    9
    4140
    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.
    • AceNinjaFire
      AceNinjaFire last edited by AceNinjaFire

      Been trying to get an idea of mine working. I made a script to download galleries, and I’m trying to set the image view after the image has been downloaded.
      But for some reason what I did below doesn’t do anything. Could someone tell me what I’m doing wrong?

      import ui,requests,Image
      from io import BytesIO
      Image_url =“some image url”
      
      image_data = BytesIO(requests.get(image_url).content)
      
      image = Image.open(image_data)
      
      w,h = ui.get_screen_size()
      testview = ui.View()
      testview.frame = (0,0,w,h)
      
      Img_view = ui.ImageView()
      
      Img_view.frame() = testview.frame()
      
      testview.add_subview(img_view)
      
      testview.present()
      
      Img_view.image= ui.Image.from_data(image.tobytes())
      
      
      
      
      1 Reply Last reply Reply Quote 0
      • AceNinjaFire
        AceNinjaFire last edited by

        Nvm I think I figured it out, it has to be a bytes-like object. I just have to figure out how to get image.tobytes() to byte-like and not bytes

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

          @AceNinjaFire said:

          import ui,requests,Image
          from io import BytesIO
          Image_url =“some image url”

          image_data = BytesIO(requests.get(image_url).content)

          image = Image.open(image_data)

          w,h = ui.get_screen_size()
          testview = ui.View()
          testview.frame = (0,0,w,h)

          Img_view

          why not use ui.ImageView.load_from_url(URL)

          1 Reply Last reply Reply Quote 1
          • stephen
            stephen @AceNinjaFire last edited by

            @AceNinjaFire
            or better yet

            
            import urllib.request, ui
            
            URL = 'https://i.imgur.com/qaAHDac.png'
            
            with urllib.request.urlopen(URL) as url:
            	with open('temp.jpg', 'wb') as f:
            		f.write(url.read())
            		
            	iv=ui.ImageView(image=ui.Image.named('temp.jpg')).present('sheets')
            
            
            AceNinjaFire 1 Reply Last reply Reply Quote 1
            • ccc
              ccc last edited by ccc

              import io, requests, ui
              url = "https://www.python.org/static/community_logos/python-powered-w-200x80.png"
              with io.BytesIO(requests.get(url).content) as image_data:
                  image = ui.Image.from_data(image_data.getvalue())
              ui.ImageView(image=image).present()
              

              If you are dealing with lots of images or large images in Pythonista then it is super important to use the with block to automate the close of the io.BytesIO and give the garbage collector the opportunity to reclaim the bytes and avoid out-of-memory crashes.

              AceNinjaFire 1 Reply Last reply Reply Quote 2
              • AceNinjaFire
                AceNinjaFire @stephen last edited by

                @stephen @ccc

                Shiiiiit sorry lol I had not realized that anyone had replied. I knew I could do that from the beginning, I was just trying to go from jpeg/image object straight to ui.Image/Ui.ImageView.

                I ended up just making a list from each requests.get().content and using that.
                I was just trying to swim up river to see if I could do it I guess lol.

                stephen 1 Reply Last reply Reply Quote 0
                • AceNinjaFire
                  AceNinjaFire @ccc last edited by AceNinjaFire

                  @ccc

                  Ah thank you lol, I got as far as image_data = BytesIO(requests.get().content)

                  I never thought about using the Context Manager and putting the “.getvalue()” on the image_data. Lol I appreciate the help!

                  1 Reply Last reply Reply Quote 0
                  • stephen
                    stephen @AceNinjaFire last edited by

                    @AceNinjaFire

                    no problem and dont forget that ui.Image.from_data() has two arguments. data and scale
                    the second (optional) is for Retina Scaling 2:1 or 3:1 usually 2:1 and you would pass 2 for scale.

                    ui.Image.from_data(image_data.getvalue(), 2)

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

                      This post is deleted!
                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post
                      Powered by NodeBB Forums | Contributors