如何解决查找图像pygame的路径
我正在尝试在pygame上制作一个显示不同图像的游戏,并且用户应通过语音识别为图像提供答案。我上载和显示图像时遇到困难,建议我使用函数来加载图像。但是现在我很难找到图像文件,并且不断出现此错误:
我检查了文件的位置和路径,对我来说一切都很好:
import os
import glob
import pygame
import time
import speech_recognition as sr
pygame.init()
display_width = 800
display_height = 600
black = (0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0)
green = (0,200,0)
bright_red = (255,0)
bright_green = (0,0)
gamedisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GUI Speech Recognition')
current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path,'test')
image_path = os.path.join(resource_path,'MG')
#image_list = []
#for filename in glob.glob('MG/*.jpg'):
#image_list.append(filename)
def close():
pygame.quit()
quit()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',30)
TextSurf,TextRect = text_objects(text,largeText)
TextRect.center = ((display_width/2),(display_height/2))
gamedisplay.blit(TextSurf,TextRect)
pygame.display.update()
def text_objects(text,font):
textSurface = font.render(text,True,alpha)
return textSurface,textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gamedisplay,(x,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gamedisplay,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf,textRect = text_objects(msg,smallText)
textRect.center = ( (x+(w/2)),(y+(h/2)) )
gamedisplay.blit(textSurf,textRect)
def load_the_image(image):
return pygame.image.load(os.path.join(image_path,image))
images = [
load_the_image('cat.jpg'),load_the_image('monkey.jpg'),load_the_image('dog.jpg')
]
#WINDOW = pygame.display.set_mode((800,800))
def s2t():
# gamedisplay.blit(carImg,(0,0))
r = sr.Recognizer()
with sr.Microphone() as source:
print ('Say Something!')
audio = r.listen(source)
print ('Done!')
text = r.recognize_google(audio)
print(text)
for i in range(len(images)):
# WINDOW.blit(i)
if i == 1:
index=0
gamedisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gamedisplay.blit(carImg,(130,0))
if text == 'cat':
message_display('good job')
else:
message_display('wrong')
elif i== 2:
index=1
gamedisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gamedisplay.blit(carImg,0))
if text == 'monkey':
message_display('good job')
else:
message_display('wrong')
elif i== 3:
index=2
gamedisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gamedisplay.blit(carImg,0))
if text == 'dog':
message_display('good job')
else:
message_display('wrong')
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Speak!",150,450,100,50,green,bright_green,s2t)
button("Quit",550,red,bright_red,close)
pygame.display.update()
if __name__ == '__main__':
main()
解决方法
子目录“ test”在图像文件路径中位于两次。
Couldn't open "/User/naifail/Desktop/test/test/MG/cat.jpg"
但是目录“ test”仅在脚本文件的路径中包含一次:
/User/naifail/Desktop/test/game.py
跳过行resource_path = os.path.join(current_path,'test')
:
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path,'MG')
def load_the_image(image):
return pygame.image.load(os.path.join(image_path,image))
,
Rabbid76所说的行之有效,但这可能是另一个简单的错误,例如图像已损坏,或者您可能必须在代码中输入实际的路径名,而不仅仅是cat.jpg。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。