Как тонировать изображение PNG с помощью альфа-канала? - PullRequest
0 голосов
/ 11 февраля 2019

Я пытаюсь закодировать типичную заставку для DVD.Я доволен этим, но я хочу менять цвет логотипа каждый раз, когда он попадает на стену.Я использовал fill (), но логотип меняется на цветной прямоугольник.Я хочу изменить цвет логотипа, соблюдая альфа-канал изображения.

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

1 Ответ

0 голосов
/ 11 февраля 2019

Сначала вы должны создать изображение с альфа-каналом, чтобы сделать прозрачную область изображения PNG невидимой.Используйте pygame.Surface.convert_alpha () Чтобы создать поверхность с альфа-каналом:

tintImage = image.convert_alpha()

Чтобы тонировать изображение с помощью pygame.Surface.fill(), special_flags тоже должен быть установлен на BLEND_RGBA_MULT.Это приводит к тому, что все пиксели изображения умножаются на цвет, а не устанавливаются на цвет:

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

Примечание. Поскольку изображение должно быть окрашено в разные цвета, исходное изображение должнобыть сохраненнымИспользуйте функцию logo, чтобы скопировать изображение, и «подкрасьте» «блиц» копию изображения:

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...