Python 如何使用编码器在PsychoPy上呈现jpg图像?

Python 如何使用编码器在PsychoPy上呈现jpg图像?,python,psychopy,Python,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 sop

我正在尝试将文本覆盖在图像上。文本是有效的,但当我添加图像刺激时,心理变态一直给我错误信息,即使我已经正确识别了路径。为什么会这样

这是我的代码:

# 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错误)'UnicodeScape'编解码器无法解码位置2-3的字节:截断\uxxxxx转义

查看包含子字符串的路径
\u
。这就是导致错误的原因,也是错误消息中提到的原因。由于该字符串不是原始字符串,因此正在对其进行分析,以获取以
\
开头的转义。要避免这种情况,请在字符串文字上使用
r
前缀,使其成为原始字符串:

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

避免这种情况的其他方法是使用转义反斜杠
\\
,或者Windows也接受
/

错误消息是什么?请编辑您的问题以包含错误消息。嗨@MarkStewart,我刚刚做了!我希望这澄清了这个问题,我相信你的答案可以在这里找到:而且,如果你看你的代码,你会看到“Stim”这个词被你的语法高亮显示为特殊。一般来说,这是一个提示,您可能正在使用一个受保护的单词作为变量名,最好改为其他名称,例如“sophistim”或“stim”,即使在这种特殊情况下,这不是问题的原因。