Jump to content
  • Advertisement
Sign in to follow this  
mbarb

pygame.image.load and os.path.join?

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am a noob so please bare with me. I have tried using pygame.image.load and I have tried using os.path.join. I used the same code as in the tutorials that I have been reading and I cannot get an image to load. The example code I am using is as follows: >>>import pygame, os, sys >>>from pygame.locals import* >>>pygame.init() (6,o) >>>window=pygame.display.set_mode((468, 60)) >>>screen=pygame.display.get_surface() >>>monkey_head_file_name=os.path.join("data","chimp.bmp") >>>print monkey_head_file_name data\chimp.bmp >>>monkey_surface=pygame.image.load(monkey_head_file_name) Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel- monkey_surface=pygame.image.load(monkey_head_file_name) error: Couldn't open data\chimp.bmp Even if I try using my own images I still get the same error. If anyone could point me in the right direction I would really appreciate it. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Do have your information store in a folder name data and located in the same directory that your are running the program?

Share this post


Link to post
Share on other sites
Let's say your script is named chimp.py and is stored in a directory C:\Documents and Settings\mbarb\My Documents\Chimp1\. For your code to work, your image needs to be located at:
C:\Documents and Settings\mbarb\My Documents\Chimp1\data\chimp.bmp

The part that is underlined above is the relative path from where your script is located - it's like directions that say "how do I get to there from here?"

Once you have that set up right, your program will work.

Share this post


Link to post
Share on other sites
Thank you both very much. I thought that might be it but I'm having a problem figuring out how to do that. I've looked through lots of tutorials and that's the code they give but they don't explain how to set it up. So I guess that's my next question. How do I change the directory?

Share this post


Link to post
Share on other sites
os.getcwd() will tell you the current working directory (where you are)
os.chdir(path) will change the current working directory to path

Share this post


Link to post
Share on other sites
Quote:
Original post by mbarb
Thank you both very much. I thought that might be it but I'm having a problem figuring out how to do that. I've looked through lots of tutorials and that's the code they give but they don't explain how to set it up. So I guess that's my next question. How do I change the directory?

It's not about changing the directory. It's not even about the code. It's about where you put files, how you structure your project.

When you run a program - almost any program in any programming language - the current directory for the program is the directory the program is in. In your case, the "program" is your script. However, you're just running individual statements in the Python interpreter, so the default current directory is actually your home directory.

You can either follow Hollower's advice or specify the full path to the file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!