pythonpygame

How to load image using pygame.image.load()?


I just want to know the syntax. How do I load an image using pygame.image.load() ? Let's take an example, I want to load an image called cat.png - and type this

pygame.image.load('cat.png')

Then, where should the image cat.png be saved?


Solution

  • By default, Python only searches in your current directory for files, however, if you wish to load images from a separate directory, you may find this useful: Importing images from a directory (Python)

    For the syntax, I reccomend you refer to the documentation, here: https://www.pygame.org/docs/ref/image.html#pygame.image.load

    pygame.image.load()¶
        load new image from a file
        load(filename) -> Surface
        load(fileobj, namehint="") -> Surface
    
    Load an image from a file source. You can pass either a filename or a Python file-like object.