pygame.transform makes new Surfaces from old ones: bigger, smaller, rotated or mirrored. Each function returns a new Surface and leaves the original alone.
From the left: the original, scaled to 96 by 96, flipped horizontally, and rotated 30 degrees.
The functions
| Function | Returns |
|---|---|
scale(surface, (w, h)) | A resized copy. Fast, keeps hard pixel edges |
scale_by(surface, factor) | A copy scaled by a factor (pygame-ce and recent pygame versions) |
smoothscale(surface, (w, h)) | A resized copy with smoothing, better for photos |
rotate(surface, angle) | A copy rotated angle degrees counterclockwise |
rotozoom(surface, angle, scale) | Rotated and scaled at once, with smoothing |
flip(surface, flip_x, flip_y) | A mirrored copy: flip(img, True, False) faces the other way |
Transforming is slow compared with blitting. Make the copies you need once, before the loop (a left-facing and a right-facing player, say), and only transform inside the loop when the result keeps changing, like a turning ship.
Rotating around the center
A rotated image is a bigger rectangle than the original (its corners stick out), so blitting it at the same top-left corner makes it wobble. Place it by the center instead, and always rotate the original:
Press Play, click the game, and turn the ship with Left and Right. The outline shows the rotated image's Rect growing and shrinking while the ship stays put.
If instead you write ship = pygame.transform.rotate(ship, 4) every frame, each rotation smears the pixels a little more and adds empty border, so after a few seconds the image is blurry and huge.
Facing left and right
Most character art faces one way. Flip it once and pick the copy by direction:
right = pygame.image.load("player.png").convert_alpha()
left = pygame.transform.flip(right, True, False)
image = left if direction < 0 else right
screen.blit(image, rect)
Pixel art
scale copies pixels without smoothing, which keeps pixel art crisp when you make it two or three times bigger. Use whole number factors (scale_by(img, 3)); a scale such as 1.5 makes some pixels wider than others. smoothscale and rotozoom blur, which suits photos and smooth art but softens pixel art.
Frequently Asked Questions
How do I resize an image in pygame?
pygame.transform.scale(image, (width, height)) returns a resized copy. In pygame-ce, pygame.transform.scale_by(image, 2) doubles it. Use smoothscale for photos; scale keeps pixel art sharp.
How do I rotate an image around its center?
Rotate the original, then place the result by the old center: rotated = pygame.transform.rotate(original, angle) and rect = rotated.get_rect(center=old_rect.center). The rotated image is bigger, so its top-left corner moves.
Which way does pygame.transform.rotate turn?
Counterclockwise for a positive angle, in degrees. Use a negative angle to turn clockwise.
Why does my rotated image get blurry and grow?
You are rotating the already rotated image every frame. Each rotation adds blur and padding. Keep the original and rotate it by the total angle each time.