pygame.transform

Resize, rotate, flip and filter Surfaces. Every function returns a new Surface and leaves the original unchanged.

2 min readUpdated
On this page

Overview#

pygame.transform creates modified copies of Surfaces. Because every call allocates a new Surface, transform images once at load time where possible, and always transform from the original image rather than from an already-transformed copy.

Syntax#

Python
pygame.transform.scale(surface, (width, height))
pygame.transform.scale_by(surface, factor)             # factor or (x, y)
pygame.transform.smoothscale(surface, (width, height))
pygame.transform.rotate(surface, angle)                 # degrees, counter-clockwise
pygame.transform.rotozoom(surface, angle, scale)
pygame.transform.flip(surface, flip_x, flip_y)

Functions#

FunctionReturns
scale(surface, size)Resized copy using nearest-neighbour sampling: fast and crisp, ideal for pixel art.
scale_by(surface, factor)Resized by a factor instead of a size.
smoothscale(surface, size)Resized with filtering for smoother results. Works on 24- and 32-bit Surfaces.
smoothscale_by(surface, factor)Filtered resize by a factor.
rotate(surface, angle)Rotated copy, larger than the original so corners fit. Positive angles turn counter-clockwise on screen.
rotozoom(surface, angle, scale)Rotate and scale with filtering in one step; smoother than rotate.
flip(surface, flip_x, flip_y)Mirrored copy.
scale2x(surface)Doubles size with an edge-preserving algorithm for pixel art.
grayscale(surface)Greyscale copy.
chop(surface, rect)Removes a region (rows and columns).
average_color(surface, rect=None)Average colour of the Surface or an area.

Rotating around the centre#

rotate() returns a bigger Surface. If you blit it at the old top-left, the sprite wobbles. Re-centre a fresh rect instead:

Python
rotated = pygame.transform.rotate(original_image, angle)
rect = rotated.get_rect(center=old_rect.center)
screen.blit(rotated, rect)

Practical example#

transform_example.pyDownload
"""pygame.transform: scale, smoothscale, rotate, rotozoom, flip, and rotating around a centre."""
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 360))
clock = pygame.time.Clock()

original = pygame.Surface((32, 20), pygame.SRCALPHA)
pygame.draw.polygon(original, (244, 197, 49), [(0, 0), (32, 10), (0, 20)])
pygame.draw.rect(original, (91, 147, 230), (0, 6, 10, 8))

big_pixel = pygame.transform.scale(original, (128, 80))          # nearest neighbour: crisp pixels
big_smooth = pygame.transform.smoothscale(original, (128, 80))   # filtered: smooth edges (24/32-bit only)
mirrored = pygame.transform.flip(big_pixel, True, False)          # flip_x, flip_y
angle = 0

running = True
while running:
    dt = clock.tick(60) / 1000
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    angle = (angle + 90 * dt) % 360

    screen.fill((20, 26, 46))
    screen.blit(big_pixel, (20, 20))
    screen.blit(big_smooth, (170, 20))
    screen.blit(mirrored, (320, 20))
    # Always rotate the ORIGINAL image, then re-centre the new, larger rect.
    rotated = pygame.transform.rotate(big_pixel, angle)
    rect = rotated.get_rect(center=(160, 240))
    screen.blit(rotated, rect)
    pygame.draw.rect(screen, (90, 110, 150), rect, 1)               # the rect grows as it rotates
    zoomed = pygame.transform.rotozoom(original, -angle, 2.5)       # rotate + scale with filtering
    screen.blit(zoomed, zoomed.get_rect(center=(460, 240)))
    pygame.display.flip()

pygame.quit()

Common errors#

  • Image gets blurrier or bigger over time: you rotate or scale the result of the previous frame. Keep the original and transform it each time with the total angle.
  • ValueError: Only 24-bit or 32-bit surfaces can be smoothly scaled: convert the Surface first (convert() or convert_alpha()), or use scale.
  • Rotation direction confusion: transform.rotate with a positive angle turns counter-clockwise on screen, while Vector2.rotate with a positive angle appears clockwise on screen, because y points down. See pygame.math.
  • Slow frame rate: transforming large images every frame is expensive. Pre-compute rotations (for example 36 angles) if many objects rotate.

pygame-ce notes#

pygame-ce adds filters such as pygame.transform.box_blur, gaussian_blur and invert, which are not in the original pygame.

Official documentation#

↑ ↓ to moveEnter to openEsc to close