1

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

Sophie Tan
  • 27
  • 1
  • 5
  • What are the error messages? Please edit your question to include the error message(s). – Mark Stewart Feb 21 '19 at 15:55
  • Hi @MarkStewart, I just did! I hope that clarifies the question – Sophie Tan Feb 21 '19 at 15:58
  • I believe your answer can be found here: [link](https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca) – Xion Feb 21 '19 at 16:26
  • Also, if you look at your code you will see the word "Stim" is being highlighted as special by your syntax highlighter. In general, that is a hint you may be using a protected word for a variable name, and it would be good policy to change to something else, e.f. "sophieStim" or just "stim" even if in this particular case it is not the cause of your problem. – brittAnderson Feb 21 '19 at 17:53

1 Answers1

1

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

Dan D.
  • 73,243
  • 15
  • 104
  • 123