【问题标题】:How to tint a png image with an alpha channel? [duplicate]如何使用 alpha 通道为 png 图像着色? [复制]
【发布时间】:2019-02-11 15:22:13
【问题描述】:

我正在尝试编写典型的 dvd 弹跳屏幕保护程序。我对它很满意,但我想在每次它撞到墙上时改变标志的颜色。我使用了 fill(),但徽标变为彩色矩形。我想根据图像的 Alpha 通道更改徽标的颜色。

from pygame import *
import random

#set canvas size variables
width = 700
height = 450

#draw canvas
screen = display.set_mode((width,height))
display.set_caption('Graphics')

#initial XY coordinates where game starts
x = random.randint(1, width)
y = random.randint(1, height)

#import logo
logo_img = image.load('dvd_logo_alpha.png')

R = 255
G = 255
B = 255

def logo(x,y):
    screen.blit(logo_img, (x,y))

def Col():
    R = random.randint(100,255)
    G = random.randint(100,255)
    B = random.randint(100,255)

#speed of the logo
dx = 3
dy = 3

endProgram = False

while not endProgram:
    for e in event.get():
        if e.type == QUIT:
            endProgram = True

    #speed changes position XY
    x += dx
    y += dy

    #detection of collision with border of screen
    if y<0 or y>height-47:
        dy *= -1
        R = random.randint(100,255)
        G = random.randint(100,255)
        B = random.randint(100,255)
    if x<0 or x>width-100:
        dx *= -1
        R = random.randint(100,255)
        G = random.randint(100,255)
        B = random.randint(100,255)

    screen.fill((0))
    logo_img.fill((R,G,B)) #here is the problem I can not solve
    logo(x,y)
    display.update()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    首先您必须创建一个带有 Alpha 通道的图像,以使 png 图像的透明区域不可见。使用 pygame.Surface.convert_alpha() 创建一个带有 Alpha 通道的表面:

    tintImage = image.convert_alpha()
    

    要通过pygame.Surface.fill() 为图像着色,special_flags 也必须设置为BLEND_RGBA_MULT。这导致图像的所有像素都与颜色相乘,而不是由颜色设置:

    tintImage.fill((R, G, B, 255), None, BLEND_RGBA_MULT)
    

    注意,由于图像必须用不同的颜色着色,因此必须保留原始图像。使用函数logo 复制图像,并对图像的副本“着色”和“blit”:

    def logo(x, y, color):
        tintImage = logo_img.convert_alpha()
        tintImage.fill((R, G, B, 255), None, BLEND_RGBA_MULT)
        screen.blit(tintImage, (x, y))
    

    在程序的主循环中调用函数:

    endProgram = False
    while not endProgram:
        for e in event.get():
            if e.type == QUIT:
                endProgram = True
    
        #speed changes position XY
        x += dx
        y += dy
    
        #detection of collision with border of screen
        if y<0 or y>height-47:
            dy *= -1
            R = random.randint(100,255)
            G = random.randint(100,255)
            B = random.randint(100,255)
        if x<0 or x>width-100:
            dx *= -1
            R = random.randint(100,255)
            G = random.randint(100,255)
            B = random.randint(100,255)
    
        screen.fill((0))
    
        #logo_img.fill((R,G,B)) #here is the problem I can not solve
        logo(x, y, (R, G, B))
    
        display.update()
    

    【讨论】:

    • 非常感谢!现在可以了!!我学到了很多东西。谢谢:)
    • 您好,我想知道 tintImage.fill((R, G, B, 255), None, BLEND_RGBA_MULT) 是否对性能有影响 我看到您正在循环中运行此代码一次但是如果我的代码中多次出现这种情况,那会很糟糕吗?
    猜你喜欢
    • 2012-06-23
    • 2015-02-06
    • 2011-06-30
    • 2018-02-11
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多