Skip to main content
 首页 » 编程设计

python之如何使用 alpha channel 给 png 图像着色

2024年11月24日16jyk

<分区>

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

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