Problem with alpha channels in my picture (PyGame)

Question:

Currently, I’m trying to paste a picture to my pygame game and this picture have a alpha channel (as you can see bellow this sentence).

Original Picture

But, for some reason, when I use convert() or convert_alpha(), it’s not putting correctly the picture with the alpha channel on the game… Even when I tried to do some mainpulations of the picture, nothing.

What happened (with the manipulations)

Here is the code I tried to write (with the manipulations for alpha channel that it didn’t worked) :

class Spritesheet:
    # utility class for loading and parsing spritesheets
    def __init__(self, filename):
        self.spritesheet = pygame.image.load(filename).convert()

    def get_image(self, x, y, width, height):
        # grab an image out of a larger spritesheet
        image = pygame.Surface((width, height))
        image.fill(pygame.color.Color("black"))
        image.blit(self.spritesheet, (0, 0), (x, y, width, height))
        image.set_colorkey(pygame.color.Color("black"))
        return image

How can I put a picture who have originally the alpha channel on it?

Asked By: Jouca

||

Answers:

Ensure that image has a transparency information. If the background isn’t transparent, you can’t magically get it (except the background has a uniform color). See Pygame image transparency confusion


You have to use convert_alpha instead of convert:

self.spritesheet = pygame.image.load(filename).convert()

self.spritesheet = pygame.image.load(filename).convert_alpha()

Use convert_alpha() to create a copy of the Surface with an image format that provides alpha per pixel. When using convert, the alpha channel and the transparency of the image is lost.

Additionally create a Surface with a per pixel alpha format. Set the SRCALPHA flag to create a surface with an image format that includes a per-pixel alpha:

image = pygame.Surface((width, height))

image = pygame.Surface((width, height), pygame.SRCALPHA)
class Spritesheet:
    # utility class for loading and parsing spritesheets
    def __init__(self, filename):
        self.spritesheet = pygame.image.load(filename).convert_alpha()

    def get_image(self, x, y, width, height):
        # grab an image out of a larger spritesheet
        image = pygame.Surface((width, height), pygame.SRCALPHA)
        image.fill(pygame.color.Color("black"))
        image.blit(self.spritesheet, (0, 0), (x, y, width, height))
        return image

Alternatively you can create a sub-surface with pygame.Surface.subsurface. See How can I crop an image with Pygame?.

class Spritesheet:
    # utility class for loading and parsing spritesheets
    def __init__(self, filename):
        self.spritesheet = pygame.image.load(filename).convert_alpha()

    def get_image(self, x, y, width, height):
        return image.subsurface(pygame.Rect(x, y, width, height))
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.