How to Make an Image with a Transparent Backround in Pygame

How to make a surface with a transparent background in pygame

This should do it:

image = pygame.Surface([640,480], pygame.SRCALPHA, 32)
image = image.convert_alpha()

Make sure that the color depth (32) stays explicitly set else this will not work.

Transparent Image in Pygame

First, your image/surface needs to use per-pixel alpha, therefore call the convert_alpha() method when you load it. If you want to create a new surface (as in the example), you can also pass pygame.SRCALPHA to pygame.Surface.

The second step is to create another surface (called alpha_surface here) which you fill with white and the desired alpha value (the fourth element of the color tuple).

Finally, you have to blit the alpha_surface onto your image and pass pygame.BLEND_RGBA_MULT as the special_flags argument. That will make
the opaque parts of the image translucent.

import pygame as pg

pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue2')
BLACK = pg.Color('black')

# Load your image and use the convert_alpha method to use
# per-pixel alpha.
# IMAGE = pygame.image.load('circle.png').convert_alpha()
# A surface with per-pixel alpha for demonstration purposes.
IMAGE = pg.Surface((300, 300), pg.SRCALPHA)
pg.draw.circle(IMAGE, BLACK, (150, 150), 150)
pg.draw.circle(IMAGE, BLUE, (150, 150), 130)

alpha_surface = pg.Surface(IMAGE.get_size(), pg.SRCALPHA)
# Fill the surface with white and use the desired alpha value
# here (the fourth element).
alpha_surface.fill((255, 255, 255, 90))
# Now blit the transparent surface onto your image and pass
# BLEND_RGBA_MULT as the special_flags argument.
IMAGE.blit(alpha_surface, (0, 0), special_flags=pg.BLEND_RGBA_MULT)

done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True

screen.fill((50, 50, 50))
pg.draw.rect(screen, (250, 120, 0), (100, 300, 200, 100))
screen.blit(IMAGE, (150, 150))

pg.display.flip()
clock.tick(60)

pg.quit()

How can I make an Image with a transparent Backround in Pygame?

The issue is the image format. JPEG images have no alpha channel. You can set a transparent colorkey by set_colorkey(), but the result will not satisfy you, because the JPEG is not lossless. That means due the compression, the colors will slightly change and the color key will not work correctly. For instance white color:

self.image = image = pygame.image.load("mew.jpg")
self.image.set_colorkey((255, 255, 255))

Either use set_colorkey() and a lossless image format like BMP:

self.image = image = pygame.image.load("mew.bmp")
self.image.set_colorkey((255, 255, 255))

or us the PNG format. For instance:

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

png images with transparent background don't work in pygame 2.0.0

PNGs have per pixel alpha. It is not necessary to set a color key. Simply blit the protector on the tank. convert() changes the pixel format to a format without alpha per pixel and removes the transparency information. Use convert_alpha() instead:

class Player(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__(player_group, all_sprites)
slef.image = pygame.image.load('baloon.png').convert_alpha()
self.image = pygame.transform.scale(slef.image, (42, 94))

Sample Image


I suggest to modify the load_image function:

def load_image(name, per_pixel_alpha = False, color_key = None):
fullname = os.path.join('textures', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)

if per_pixel_alpha:
image = image.convert_alpha()
else:
image = image.convert()

if color_key is not None:
if color_key == -1:
color_key = image.get_at((0, 0))
image.set_colorkey(color_key)

return image
class Player(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__(player_group, all_sprites)
self.image = pygame.transform.scale(load_image('baloon.png', True), (42, 94))

self.rect = self.image.get_rect().move(pos_x, pos_y)

def update(self, *args):
pass

Pygame image transparency confusion

You have to use an image with a transparent background. Be careful and do not download a preview of an image with a checkered background. Just because they are PNGs doesn't mean the background is transparent. The background can still be opaque. Use the following image

Sample Image


If you copy a transparent Surface to another Surface the target Surface has to provide transparency respectively per pixel alpha.

You can enable additional functions when creating a new surface. Set the SRCALPHA flag to create a surface with an image format that includes a per-pixel alpha. The initial value of the pixels is (0, 0, 0, 0):

my_surface = pygame.Surface((width, height), pygame.SRCALPHA)

and adapt the method image_at of the class SpriteSheet. Use pygame.SRCALPHA:

class SpriteSheet:
# [...]

def image_at(self, rectangle, colorkey = None):
"""Load a specific image from a specific rectangle."""
# Loads image from x, y, x+offset, y+offset.
rect = pygame.Rect(rectangle)

image = pygame.Surface(rect.size, pygame.SRCALPHA) # <----

image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image

Or use convert_alpha():

class SpriteSheet:
# [...]

def image_at(self, rectangle, colorkey = None):
"""Load a specific image from a specific rectangle."""
# Loads image from x, y, x+offset, y+offset.
rect = pygame.Rect(rectangle)

image = pygame.Surface(rect.size).convert_alpha() # <----
image.fill((0, 0, 0, 0)) # <---

image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image

See also:

  • How can I make an Image with a transparent Backround in Pygame?
  • How do I blit a PNG with some transparency onto a surface in Pygame?

Note that chess pieces can also be drawn through a Unicode text.

See Displaying unicode symbols using pygame



Related Topics



Leave a reply



Submit