如何将pygame表面的某些颜色列入白名单



有没有一种方法可以在pygame中将表面上的颜色列入白名单,并且任何不在该白名单中的颜色都会变成透明的?

我试着使用icon.set_colorkey(),但只删除了一种颜色的

您必须手动执行此操作。确保图像的格式具有pygame.Surface.convert_alpha()的alpha通道。识别具有高于特定阈值(例如230(的红-绿和蓝颜色通道的所有像素。将这些像素的颜色通道和alpha通道更改为(0,0,0(:

img = pygame.image.load(IMAGE).convert_alpha()
threshold = 230
for x in range(img.get_width()):
for y in range(img.get_height()):
color = img.get_at((x, y))
if color.r > threshold and color.g > threshold and color.b > threshold:
img.set_at((x, y), (0, 0, 0, 0))

最新更新