Forum Archive

How to load an image into image view?

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())



AceNinjaFire

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

@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)

stephen

@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')

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

@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.

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!

stephen

@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)

Caitlins

we are having mobile applications for each and every task that we do throughout the day. And we love to use the application that is more interactive and have better UI.