Hey guys, recently I started playing around with pygame. Right now im making a little orb move when you hit a key. But its stuck with the image loading!

background_image_filename = 'C:\\orb\\backroundorb.jpeg'
sprite_image_filename = 'C:\\orb\\orbsprite.jpeg'

import pygame
from pygame.locals import *
from sys import exit

pygame.init()



screen = pygame.display.set_mode((640, 480), 0, 32)

background = pygame.image.load(background_image_filename)
sprite = pygame.image.load(sprite_image_filename)

does anyone see the problem with this part of the code? It comes up with the error message

Traceback (most recent call last):
File "C:\Python25\orb", line 14, in <module>
background = pygame.image.load(background_image_filename)
error: Couldn't open C:\orb\backroundorb.jpeg

There is a folder called orb and there are images called
backroundorb.jpeg and orbsprite.jpeg.

Can anyone help me with this?

Dani AI

Generated

A few quick, practical notes based on this thread — good job debugging, , and thanks to and for the helpful hints.

If a library call says it cannot open an image file, the usual culprits are simple: a misspelled filename, the path your script is actually using (current working directory vs. where you think the file lives), escaping problems in string literals on Windows, permission issues, a corrupted file, or lack of codec support for that image format. Start with the smallest checks first: confirm the exact filename and extension, and open the file in a normal image viewer to rule out corruption.

Two practical checks that quickly find the problem: (1) list the files in the folder the program is looking at and confirm the image is present with the exact name you use, and (2) verify the Python process can read the file (the running user must have read permission). Remember that when you run code from an IDE or from a shortcut the working directory can differ; absolute paths avoid that, and platform helpers reduce escaping pain.

A couple of environment notes: some builds of the image-support library used by pygame lack certain codecs (JPEG in some cases) — trying a PNG copy is a quick way to test that. Also, call surface conversion functions only after a display mode is set. For reference, the pygame image documentation and Python filesystem helpers are good resources: Pygame image docs and Python os.path.exists.

Credit to for spotting the common-typo angle and to for recommending direct file-access checks — combining those moves usually isolates these errors fast.

Recommended Answers

All 7 Replies

Is your file named backroundorb.jpeg or backgroundorb.jpeg

In my file name i mispelled background and spelled it backround. I was to lazy to fix it so its still backroundorb.jpeg

Hi rysin,

Can you open the file at all from within python? If you say

f = open("C:\\orb\\backroundorb.jpeg", "r")

does that work?

No it doesnt haha! What do you think is wrong with it? IM on a compaq computer... do I put that in the path name?

Hi rysin,

Maybe try importing os and run os.listdir() on the given directory. Do the following and please tell me what you get:

import os
os.listdir("C:\\orb")

Or maybe it could be some kind of weirdness in processing the backslashes? I'm not at a Windows PC, so I can't tell.

THis is what I got

>>>
>>>
It just skipped to a new line

YES! After many many hours(3 to be exact!) of debugging I found the problem. It was actually later in the program! Thanks to all who helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.