How to load an image into image view?
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())
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
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)
why not use ui.ImageView.load_from_url(URL)
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')
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
withblock 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.
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.
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!
no problem and dont forget that
ui.Image.from_data()has two arguments.
the second (optional) is for Retina Scaling
2:1and you would pass
This post is deleted!last edited by