簡體   English   中英

如何將 pygame 表面中的某些顏色列入白名單

[英]How to whitelist certain colours in a pygame surface

有沒有辦法將 pygame 表面上的顏色列入白名單,並且任何不在該白名單中的顏色都會變為透明?

我嘗試使用icon.set_colorkey()但這只會刪除一種顏色

您必須手動確保圖像格式具有pygame.Surface.convert_alpha()的 alpha 通道。 識別具有高於某個閾值(例如 230)的紅綠藍顏色通道的所有像素。 將這些像素的顏色通道和 alpha 通道更改為 (0, 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM