Search code examples
pythonimagepygamealpha

pygame blit the transparent part of an image to white


I'm creating a game in pygame and I'm trying to load a png image with part of the image transparent (not 100%), but pygame is blitting that image with a white color instead.

What's happening is that there's a white color when the alpha is not 100%, and fully transparent when the alpha is 100%.

enter image description here

This is the code:

import pygame
from pygame.locals import Color

pygame.init()
win = pygame.display.set_mode((width,height))

sprite_background = pygame.image.load("_0008_back.png").convert_alpha()
sprite_arm = pygame.image.load("_0007_arm.png").convert_alpha()
sprite_hand_0 = pygame.image.load("_0006_Layer-1.png").convert_alpha()

while True:
    win.blit(background, (0,0))
    win.blit(sprite_hand_0, (50,50))

I also see the printing "libpng warning: iccp: known incorrect sRGB profile" if its relevant


Solution

  • As this this answer, you're doing it alright but it need an additional step, where you copy the entire image over a white background and then multiply to get the white out:

    sprite_hand_0 = pygame.image.load("_0006_Layer-1.png").convert_alpha()
    transparent_hand = sprite_hand_0.copy()
    transparent_hand.fill((255, 255, 255, 100), special_flags=pygame.BLEND_RGBA_MULT)