In this guide, we will be taking a look at some different image effects you can use within your PyGame. In the examples below, I will be drawing sample images at x: 480 and y: 128. To learn how to position images, you can take a look at this guide of ours.

Scaling Images

You can scale images with PyGame using the pygame.transform.scale method. This method, like the other methods part of pygame.transform. For example, you can write:

scaled_myimage = pygame.transform.scale(myimage, (128, 480))

With the pygame.transform.scale method, the first argument is the image, and the second argument is a tuple representing the new size of the image.

Flipping Images

To flip an image with PyGame, we can use the pygame.transform.flip method. The first argument is the image you want to draw, the second is a boolean determining whether to flip the image horizontally, and the third is a boolean determining whether the image should be flipped vertically.

To flip an image horizontally, we could use the following line:

flipped_myimage = pygame.transform.flip(myimage, True, False)

Likewise, to flip an image vertically, we can write this line:

flipped_myimage = pygame.transform.flip(myimage, False, True)

Of course, to flip an image horizontally and vertically, you can set the second and third arguments to true.

flipped_myimage = pygame.transform.flip(myimage, True, True)

Rotating Images

To rotate images, we can use the pygame.transform.rotate method.

rotated_myimage = pygame.transform.rotate(myimage, 45)

Now, if you were to update the angle every tick, you will notice the origin is not centered. To center the origin of the image, you can add the following function. This function was originally taken from the PyGame Wiki.

def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

I think this solution is much better than anything I could come up with, which is why I recommend the solution on the PyGame Wiki.

To utilize this in your game, you could add the following to your game loop/draw function:

rotated_image = rot_center(myimage, angle)
display.blit(image, image.get_rect())

Now the image is rotating around the center! Fantastic.

Rotating and Scaling Images

You can also combine rotate and scale operations with a single method:

image = pygame.transform.rotozoom(myimage, 45, 0.5)

With the rotozoom method, the first argument is the surface (image), the second is the degrees to rotate the surface, and the last argument is the new scale.

If I wanted to rotate an image 45 degrees, and scale it 50% smaller, you could write the following line:

image = pygame.transform.rotozoom(myimage, 45, 2)

Then I would end up with the final result:

Double the Scale of an Image

Another interesting image effect we can use is the pygame.transform.scale2x method. This method doubles the scale of a surface. We just simply pass in an image, then it returns the new surface (the image twice the size.)

image = pygame.transform.scale2x(myimage)

Obviously, we can blit this new surface.

Concluding Thoughts

Hopefully this guide has helped you figure out some of the different image effects you can use with PyGame. Keep in mind, when I mentioned images in this tutorial, I really meant surfaces. You can find other guides in this series here. To learn about new tutorials and guides we release, you can subscribe to our newsletter. Sharing this guide on your social media platform of choice would also be much appreciated.