Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: PsychPy is unable to load images from a public online source. Specifically, it is unable to load an image from a public AWS s3 bucket, as in: https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg #6330

Open
fjkdasilva opened this issue Mar 15, 2024 · 4 comments
Labels
🌟 enhancement Proposed improvement or feature request. ▶️ good first issue 👀 visual Issue with presentation of visual stimuli or graphics.

Comments

@fjkdasilva
Copy link

PsychoPy Version

2023.2.3

What OS are your PsychoPy running on?

Windows 11

Bug Description

PsychPy is unable to load images from a public online source. Specifically, it is unable to load an image from a public AWS s3 bucket, as in: https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg.

As a test I tried the following code:
from psychopy import visual, core
# Create a window
win = visual.Window([800, 600])

      # Try to load the image from local source
      image = visual.ImageStim(win, image='images/happy/004504.jpg')
      image.draw()
      win.flip()
      
      **# Note: this first image is displayed just fine**
      userInput = input("press any key")
      
      
      
      # Try to load the image from S3
      print("Attempting to load image from S3")
      try:
          image = visual.ImageStim(win, image='https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg')
          print("Image loaded successfully")
      except Exception as e:
          print(f"Failed to load image: {e}")
      
      
      image.draw()
      win.flip()
      
      # Keep the window open long enough to see the image
      core.wait(5)
      win.close()

When PsychoPy tries to load the 2nd image I get the following error: press any key
Attempting to load image from S3
0.9001 WARNING Monitor specification not found. Creating a temporary one...
5.1496 ERROR Couldn't find image https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg; check path? (tried: C:\Users\flavi\Desktop\GITLAB\dotprobtask\https:\neurotype-images.s3.us-east-2.amazonaws.com\affective-images\004504.jpg)
Failed to load image: Couldn't find image https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg; check path? (tried: C:\Users\flavi\Desktop\GITLAB\dotprobtask\https:\neurotype-images.s3.us-east-2.amazonaws.com\affective-images\004504.jpg)

Expected Behaviour

I expect PsychoPy to load an image from any public source with out any problem just like my browser does.

Steps to Reproduce

run the following code:
from psychopy import visual, core
# Create a window
win = visual.Window([800, 600])

      # Try to load the image from local source
      image = visual.ImageStim(win, image='images/happy/004504.jpg')
      image.draw()
      win.flip()
      
      **# Note: this first image is displayed just fine**
      userInput = input("press any key")
      
      
      
      # Try to load the image from S3
      print("Attempting to load image from S3")
      try:
          image = visual.ImageStim(win, image='https://neurotype-images.s3.us-east-2.amazonaws.com/affective-images/004504.jpg')
          print("Image loaded successfully")
      except Exception as e:
          print(f"Failed to load image: {e}")
      
      
      image.draw()
      win.flip()
      
      # Keep the window open long enough to see the image
      core.wait(5)
      win.close()

Additional context

Thanks for your help

@fjkdasilva fjkdasilva added the 🐞 bug Issue describes a bug (crash or error) or undefined behavior. label Mar 15, 2024
@TEParsons
Copy link
Contributor

I'm going to relabel this as an enhancement as currently loading images from a URL isn't something ImageStim is supposed to be able to do - that would be a new feature rather than a fix. Would be nice though, and very possible as both requests and PIL are already in PsychoPy's core requirements, so I'll add the "Good first issue" tag too.

One thing to be aware of with this is that accessing a resource from a server means your IP is exposed. This is fine if it's e.g. the Pavlovia server, but when connecting to something like amazonaws we would definitely need to have some kind of warning. There was a case in 2022 where a German court ruled that accessing the Google Fonts server without warning violated GDPR, which is why PsychoPy warns you when you try to use a Google Font in TextStim/TextBox.

@TEParsons TEParsons added 🌟 enhancement Proposed improvement or feature request. ▶️ good first issue 👀 visual Issue with presentation of visual stimuli or graphics. and removed 🐞 bug Issue describes a bug (crash or error) or undefined behavior. labels Mar 22, 2024
@peircej
Copy link
Member

peircej commented Mar 22, 2024

Just to add... PsychoPy is designed for high-performance, very low-latency operation, so adding in the delay of fetching resources from the web when they are needed should be treated very carefully considering those goals. I would strongly recommend fetching local copies of images for your studies in advance when running in the lab

@TEParsons
Copy link
Contributor

If you definitely need to get it from online and are okay with the loss of speed from having to query a server each time, the following code should work (using url for the image url and img for the ImageStim):

# get raw data from server
raw = requests.get(url, stream=True).raw
# create a PIL image from raw data
data = pil.open(raw)
# create an image stim
img.setImage(data)

(you could also use requests.get, pil.open and pil.save to programmatically get and save all the images just once, so you can use the local files)

@fjkdasilva
Copy link
Author

fjkdasilva commented Mar 28, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 enhancement Proposed improvement or feature request. ▶️ good first issue 👀 visual Issue with presentation of visual stimuli or graphics.
Projects
None yet
Development

No branches or pull requests

3 participants