让我们看看我是否正确理解了您的问题:您想将图像“白化”到表面上,但是要通过遮罩来实现,它只允许光源的某些像素实际最终出现在表面上?
我遇到了这个确切的问题,起初我认为只有通过PIL才能实现。然而,经过一些阅读和试验,事实证明,实际上可以借助pygame相当模糊的“特殊标志”来完成此操作。下面是一个函数,希望可以完成您想要的。
def blit_mask(source, dest, destpos, mask, maskrect):
"""
Blit an source image to the dest surface, at destpos, with a mask, using
only the maskrect part of the mask.
"""
tmp = source.copy()
tmp.blit(mask, maskrect.topleft, maskrect, special_flags=pygame.BLEND_RGBA_MULT)
dest.blit(tmp, destpos, dest.get_rect().clip(maskrect))
Run Code Online (Sandbox Code Playgroud)
如果希望蒙版透明,则蒙版应为白色,否则应为黑色。
| 归档时间: |
|
| 查看次数: |
1399 次 |
| 最近记录: |