1

So i am on raspberry pi 2 and I am trying to make a game and i have the .py file in the same folder as the png but its still not working. it gives me this error: Also im using pycharm

/usr/bin/python2.7 /home/pi/Documents/QFTPC.py
Traceback (most recent call last):
  File "/home/pi/Documents/QFTPC.py", line 8, in <module>
    wafflestage1 = pygame.image.load("avi.png")
pygame.error: Couldn't open avi.png

Process finished with exit code 1

code:

import pygame
import random
pygame.init()
gameDisplay = pygame.display.set_mode((1000,700))
pygame.display.set_caption("Waffle time")
black = (0,0,0)
white = (255,255,255)
wafflestage1 = pygame.image.load("avi.png")
def message_to_screen(msg,color,x,y,fs):
    font = pygame.font.SysFont(None, fs)
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [x,y])
def drawrect(color,x,y,w,h):
    pygame.draw.rect(gameDisplay, color, (x,y,w,h))




while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    gameDisplay.fill(black)
    drawrect(white,500,70,50,50)
    message_to_screen("Hello",white,200,500,50)
    pygame.display.update()
3
  • try using the full path to the image. – Steve Robillard Mar 12 '15 at 22:54
  • what full math @SteveRobillard – user27834 Mar 12 '15 at 23:01
  • 1
    something like /home/pi/images/avi.png – Steve Robillard Mar 12 '15 at 23:03
2

The file needs to be in the "working directory", that is the directory where the code is ran. That is not necessarily where the code is, especially as you're using and IDE (pycharm).

The solution is either:

1) use a full path for the file. Not recommended as your code has a hardcoded dependency on your file system.

2) add the directory where the file is as resources so that pycharm can add it to its PYTHON_PATH variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.