Is it possible to present images directly from an url?

I would like to display image files from an online source. Can I directly display images by providing an url instead of a local filename/filepath? Passing the url like this does not work.

from psychopy import core, visual

win = visual.Window(size = [640,480])
url = "https://cs.stanford.edu/people/rak248/VG_100K/3.jpg"
img = visual.ImageStim(win=win, image=url)
img.draw()
 
win.flip()
core.wait(2.0)
win.close()
core.quit()

This results in a “couldn’t find image” error. Is it possible to use the url directly to show the image (how?) or do I need to download the images and store them locally first?

You could embed html. My Brookes template has an example of embedding a video.

It’s possible, but not without a fair bit of coding - is there any particular reason you need to use the images directly from an online source? If not I would recommend just downloading them and referring to local paths.

Thanks for the replies!

There is a huge number of image-urls which I draw randomly from, so I wanted to avoid downloading all those images. My - naive - assumption was that there may be a simple way to just use the url to the image file similar to how I could use the local file path.

My solution now is to download the image for a given trial and to overwrite this file on every trial (using urllib.request.urlretrieve(url, 'last_image.png)). This works fine.

Wow, wasn’t expecting that to work… I thought you’d have to convert the data received to a PIL before supplying it to the Image component, but I guess urlretrieve already returns it in the correct format. This is good to know, thanks!