Error using pygame.image.load ()

When I try to upload an image to another folder, I get ...

pygame.error: Failed to open sprites /testtile.png

I can download .png files just fine if they are in one directory, but as soon as they are in another folder, I get this error.

I know that python has access to this other folder, because I have no errors when importing .py files from folded ones.

When I try to load pygame.image.get_extended, it returns 0, but loading .png files from the same directory does not give me any problems, so I don't think this causes this problem.

By the way, I am launching PyCharm, and such things always seem to me to be a nuisance with this IDE. I don’t even think this is a pygame issue. I don’t know what to do at this moment.

FOLDER STRUCTURE:

Scripts / GraphicsDriver.py

sprites / testtile.png

driver tries to access testile.png file

+4
source share
1 answer

Is a sprite directory in a directory using GraphicsDriver.py? You may encounter some problems with the PyGame image downloader. It searches for files in the same directory in which PyGame was initialized. Use 'os.path.join' to point it to the absolute path to your file.

I usually write my own little image downloader around it, though, since it offers a little more flexibility to the process. Something like this that will return an image and a rectangle:

def load_image(name, colorkey = None): """loads an image and converts it to pixels. raises an exception if image not found""" fullname = os.path.join('data', name) try: image = pygame.image.load(fullname) except pygame.error, message: print 'Cannot load image:', name raise SystemExit, message image = image.convert() # set the colorkey to be the color of the top left pixel if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0,0)) image.set_colorkey(colorkey, RLEACCEL) return image, image.get_rect() 

Hope this helps.

+3
source

All Articles