What Is Convert() And Convert_Alpha()

If you have worked with images in pygame you will have noticed that often the code will look something like these two examples:

					image = pygame.image.load("image.png").convert
					image = pygame.image.load("image.png").convert_alpha()

.convert() and .convert_alpha() are appended to the end of the image.load code, but it isn’t immediately clear what these methods do.

To get a better understanding, we need to refer to the pygame documentation.

Purpose of .convert()

Pygame documentation explains that the .convert() method “Creates a new copy of the surface with the pixel format changed” and “if no arguments are passed, the new surface will have the same pixel format as the display surface”

That explanation might not make much sense, but in simple terms, when you first create the main game window using set_mode(),  Pygame creates a surface and automatically specifies the pixel format based on your PC’s display.

Any images that you load into your game and then blit onto the game window will have to be converted in real time by Pygame to make sure they match the pixel format.

This is where the .convert() method comes in. By applying this to your image when it is first loaded, it is already being converted to the required pixel format. This makes the blitting process much quicker since no additional conversion is required.

This is summed up in Pygame’s documentation, which goes on to say:

pygame convert documentation screenshot

How to Use .convert() and .convert_alpha()

So as a general rule, you should always be converting images in when you load them. But why are there two methods for doing this? What’s the difference between .convert() and .convert_alpha()?

Let’s use this image to demonstrate.

I load the image into my game and use .convert(), and convert_alpha(), then blit the image onto the screen to show the difference.

.convert() used
.convert_alpha() used

The obvious difference is what has happened to the image’s background. While convert_alpha() gives an image with a transparent background, convert() loses transparency and gives a black background instead.

convert() strips out the transparent pixels from the image, while convert_alpha() maintains them. But it is possible to use convert() and maintain transparency.

Setting Colorkey

Setting a colour as the image’s colorkey, makes that colour transparent. In the image, once it was converted, the background became black. So to add transparency, the code becomes.

					character = pygame.image.load("character.png").convert()
character.set_colorkey((0, 0, 0))

The result is the same as using convert_alpha()

So which should you use? Well there is no significant performance difference so if you don’t need transparency then use .convert(), but if you do need to maintain transparency, then convert_alpha() will convert the image and keep the transparency with one line of code.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *