我正试图在图像上叠加文字。文本工作,但当我添加图像刺激时,PsychoPy不断给我错误信息,即使我已经正确识别路径。为什么是这样?
这是我的代码:
# 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()
这是错误消息:
SyntaxError :( unicode错误)'unicodeescape'编解码器无法解码2-3位的字节:截断\ UXXXXXXXX转义
看看它包含子串\u
的路径。这是导致错误的原因,并且是错误消息中提到的内容。由于字符串不是原始字符串,因此它将被解析为以\
开头的转义符。要避免这种情况,请在字符串文字上使用r
前缀使其成为原始字符串:
sophie_image=r'C:\Users\Sophie\OneDrive\Pictures\PSYCHOPY-globeimage'
避免这种情况的其他方法是使用转义反斜杠\\
或Windows也接受/
。