【问题标题】:pygame blit the transparent part of an image to whitepygame 将图像的透明部分变为白色
【发布时间】:2020-03-03 10:49:59
【问题描述】:

我正在 pygame 中创建一个游戏,我正在尝试加载一个 png 图像,其中部分图像是透明的(不是 100%),但 pygame 正在用白色代替该图像。

发生的情况是当 alpha 不是 100% 时有白色,当 alpha 为 100% 时是完全透明的。

这是代码:

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))

如果相关,我还会看到打印“libpng 警告:iccp:已知不正确的 sRGB 配置文件”

【问题讨论】:

  • 可以使用.set_colorkey()设置透明色键。
  • 您应该将图像添加到您的问题中。否则,没有人能重现您的问题。
  • .set_colorkey() 似乎不起作用,当前图像是:i.imgur.com/uhFAz16.png 我需要白色部分透明

标签: python image pygame alpha


【解决方案1】:

就像这个this answer,你做得很好,但它需要一个额外的步骤,你将整个图像复制到白色背景上,然后乘以得到白色:

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)

【讨论】:

  • 这会导致以下错误:“特殊标志”是此函数的无效关键字参数
  • 更正:确实有效,但它使整个图像有点透明
猜你喜欢
  • 2018-07-26
  • 2018-07-18
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多