pygame.image.load() reads an image file and returns a Surface. Load images once, before the game loop, and blit them every frame:
(48, 48)
The examples on this page use the free art pack in an assets folder (images by Kenney, CC0). On your own computer, put your PNG files in a folder next to your script and change the paths.
convert() and convert_alpha()
A freshly loaded image is stored in the file's pixel format. Converting it to the screen's format makes every blit several times faster:
| Call | Use for |
|---|---|
.convert_alpha() | Images with transparency: characters, items, effects |
.convert() | Solid images: backgrounds, photos, tiles with no holes |
Both need the window, so they must come after pygame.display.set_mode(). Called before it, they fail with pygame.error: No video mode has been set.
Placing an image: get_rect()
image.get_rect() returns a Rect the size of the image. Keyword arguments place it by any point:
rect = image.get_rect(center=(240, 160))
rect = image.get_rect(midbottom=(100, 260)) # standing on a floor at y=260
rect = image.get_rect(topright=(470, 10))
Keep that Rect as the object's position, move it, and blit the image at it.
A tiled background
Small tiles repeated across the screen make a background from one image. Two loops place a copy every tile width:
The tiles are blitted once onto a background Surface before the loop, so each frame costs one blit for the whole floor. Press Play, click the game and walk with the arrow keys.
File not found
FileNotFoundError: No file 'player.png' found in working directory '/Users/sam'.
A relative path such as "player.png" is looked up from the working directory, the folder your terminal or editor ran Python from, which is often not your script's folder. Two fixes:
import os
HERE = os.path.dirname(os.path.abspath(__file__))
player = pygame.image.load(os.path.join(HERE, "images", "player.png")).convert_alpha()
or run the game from its own folder (cd mygame, then python main.py). Also check the name's capitals: Player.PNG and player.png are different files on macOS and Linux servers.
Formats
Pygame loads PNG, JPG, BMP, GIF (first frame only), TGA, WebP and more. Use PNG for game art: it keeps sharp edges and transparency. To save a Surface, for example a screenshot, use pygame.image.save(screen, "shot.png").
Next: scale, rotate and flip images, then turn them into sprites.
Frequently Asked Questions
How do I load an image in pygame?
image = pygame.image.load("player.png").convert_alpha(), after pygame.display.set_mode(). Then draw it with screen.blit(image, (x, y)).
What image formats does pygame load?
PNG and JPG everywhere, and also BMP, GIF (first frame), TGA, WebP and others. Use PNG for game art with transparency.
Why does pygame say No file found in working directory?
The path is relative to the folder you ran Python from, not the folder of your script. Build the path from the script's location: os.path.join(os.path.dirname(__file__), "player.png"), or run the script from its own folder.
Should I use convert() or convert_alpha()?
convert_alpha() for images with transparent parts (characters, items), convert() for solid images (backgrounds). Both make blitting much faster, and both must come after set_mode().