Sophie Tan
Sophie Tan

Reputation: 27

How to present a jpg image on PsychoPy using coder?

I'm trying to overlay text over an image. The text works, but when I add the image stimuli, PsychoPy keeps giving me error messages, even though I've correctly identified the path. Why is this?

This is my code:

# Import the PsychoPy libraries that you want to use
from psychopy import core, visual

# Create a window
win = visual.Window([400,300], monitor="testMonitor")

#load image
sophie_image='C:\Users\Sophie\OneDrive\Pictures\PSYCHOPY-globeimage'

# Create a stimulus for a certain window
message = visual.TextStim(win, text="Hello World!")

Stim = visual.ImageStim(win, image=sophie_image) 

# Draw the stimulus to the window. We always draw at the back buffer of the window.
message.draw()

Stim.draw()

# Flip back buffer and front  buffer of the window.
win.flip()

# Pause 5 s, so you get a chance to see it!
core.wait(5.0)

# Close the window
win.close()

# Close PsychoPy
core.quit()

This is the error message:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Upvotes: 1

Views: 1385

Answers (1)

Dan D.
Dan D.

Reputation: 74645

Look at your path it contains the substring \u. This is what causes the error and is what is mentioned in the error message. As the string is not a raw string it is being parsed for escapes that start with \. To avoid this, use the r prefix on the string literal to make it a raw string:

sophie_image=r'C:\Users\Sophie\OneDrive\Pictures\PSYCHOPY-globeimage'

The other ways to avoid this are to use the escaped backslash \\ or as Windows also accepts /.

Upvotes: 1

Related Questions