1

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

simon lav
  • 39
  • 4
  • You can use [`.set_colorkey()`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey) to set the transparent colorkey. – Rabbid76 Nov 06 '19 at 21:54
  • You should add the image to your question. Otherwise, nobody can reproduce your problem. – sloth Nov 07 '19 at 07:11
  • .set_colorkey() doesent seem to work, and the current image is: https://i.imgur.com/uhFAz16.png where I need the white part to be transparent – simon lav Nov 07 '19 at 10:51

1 Answers1

0

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)
fixmycode
  • 8,220
  • 2
  • 28
  • 43