Using this question as a reference, I am trying to display an image where one of the colors in the image is meant to be the alpha channel: PyGame: Applying transparency to an image with alpha?
My source image is a single layer PNG file where I want the black (0, 0, 0) pixels to not be drawn. Most of the time I just get the entire image fading out after using the set_alpha() command on it. That previous answer suggests that it might be a GIMP problem. If I use GIMP and set black to be the transparency color and export out the file set to Indexed color mode as directed, I do finally get transparent pixels in the correct places, however, now the color palette is incorrect (green pixels are now purple, etc.). If I export from GIMP using RGB color mode then the colors are correct, but the black pixels are still drawn by Pygame even though in GIMP they are not.
That previous answer also has a reply suggesting that you can effectively create the desired affect of choosing what pixel color to use as green-screen index by printing one image on top of another:
import pygame
pygame.init()
window = pygame.display.set_mode((200, 200))
image = pygame.image.load('alpha.png')
surface = pygame.Surface(image.get_size(), depth=24)
key = (0,255,0)
surface.fill(surface.get_rect(), key)
surface.set_colorkey(key)
surface.blit(image, (0,0))
surface.set_alpha(128)
window.fill((255,255,255))
window.blit(surface, (0,0))
pygame.display.flip()
However, if I try this method, then I get ValueError: invalid rectstyle object when executing the surface.fill(surface.get_rect(), key), so perhaps these steps are no longer valid in the current version of Pygame? If somebody can provide a more recent series of steps of correctly saving out the file in GIMP and loading it into Pygame it would be greatly appreciated.
Alright, with some guessing I figured out something that works. Starting with a PNG file that doesn't have an alpha channel saved out already and working with GIMP 2.10.36:
pygame.image.load("my_image.png").convert_alpha().The image now has the correct blending based on the
set_alpha()setting for the image.