2017-03-15 132 views
1

这听起来像是一件简单的事情,但我很挣扎。 我有一个名为“零”的透明中心的矩形的PNG图像。我想改变图像可见部分的颜色。为了尝试和改变它,我尝试过使用“zero.fill”,但没有选择,我尝试只更改非透明部分,或者将新颜色与旧颜色合并,并保持这种状态。 我并不热衷于安装numpy,因为我希望将完成的程序带给没有它的朋友。任何简单的建议欢迎。如何在不改变透明度的情况下更改Pygame中的图像颜色?

+1

发生在相同大小的需要有颜色的矩形图像.. –

+0

我试过,但无论哪种透明度不工作或它通过向下方的黑色背景彩色的矩形图像。 –

+1

你需要能够更改代码中的颜色吗?如果没有,您可以使用任何图像软件创建其他图像,并在代码中根据条件 – The4thIceman

回答

1

我有一个例子,可以使用每像素alpha表面,也可以是半透明的。 fill函数只是循环遍历曲面的像素,并将它们设置为新颜色,但保持它们的alpha值。对于具有多个曲面的每一帧,可能不建议这样做。 (按F,G,H改变颜色。)

import sys 
import pygame as pg 


def fill(surface, color): 
    """Fill all pixels of the surface with color, preserve transparency.""" 
    w, h = surface.get_size() 
    r, g, b, _ = color 
    for x in range(w): 
     for y in range(h): 
      a = surface.get_at((x, y))[3] 
      surface.set_at((x, y), pg.Color(r, g, b, a)) 


def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 

    # Uncomment this for a non-translucent surface. 
    # surface = pg.Surface((100, 150), pg.SRCALPHA) 
    # pg.draw.circle(surface, pg.Color(40, 240, 120), (50, 50), 50) 
    surface = pg.image.load('bullet2.png').convert_alpha() 
    surface = pg.transform.rotozoom(surface, 0, 2) 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_f: 
        fill(surface, pg.Color(240, 200, 40)) 
       if event.key == pg.K_g: 
        fill(surface, pg.Color(250, 10, 40)) 
       if event.key == pg.K_h: 
        fill(surface, pg.Color(40, 240, 120)) 

     screen.fill(pg.Color('lightskyblue4')) 
     pg.draw.rect(screen, pg.Color(40, 50, 50), (210, 210, 50, 90)) 
     screen.blit(surface, (200, 200)) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
    sys.exit() 

bullet2.png

+0

我已将半透明图像添加到我的答案中,以便您可以更好地看到效果。只有颜色发生变化,但透明度保持不变。 – skrx

+1

谢谢skrx,这对于复杂或主题图像会更有用。 –

+1

不久之前我需要skrx的方法。它更加灵活。再次感谢。 :-) –

1

在这个版本中,可见光和透明部分是倒过来原来的问题按ANKUR的建议。 这里是必不可少的工作代码:

import pygame, sys 
from pygame.locals import * 
pygame.init() 

def load_image(name): 
    image = pygame.image.load(name).convert() 
    return image 

def resize(obj, w, h): 
    global scale 
    return pygame.transform.scale(obj, (int(w * scale), int(h * scale))) 


pink = (255, 0, 160) 
red = (255, 0, 0) 
peach = (255, 118, 95) 
blue = (0, 0, 255) 
blue_1 = (38, 0, 160) 
dark_yellow = (255, 174, 0) 
green = (38, 137, 0) 
orange = (255, 81, 0) 
colour = [pink, red, peach, blue, blue_1, dark_yellow, green, orange, green] 
clock = pygame.time.Clock() 
scale = 4 
screen = pygame.display.set_mode((292 * scale, 240 * scale),0, 32) 
banner = load_image("banner.png") #292x35 
zero = load_image("zero.png") #5x7 
c = 0 
while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
    rgb = colour[c] 
    c = c + 1 
    if c > 7: 
     c = 0 
    pygame.draw.line(banner, rgb, (53, 21), (53, 27), 5) #line the same size as zero image 
    banner.blit(zero, (51, 21)) #blit image with transparency over line 
    large_banner = resize(banner, 292, 35) 
    screen.blit(large_banner, (0, 0)) 
    clock.tick(120) 
    pygame.display.flip() 

pygame.quit() 
sys.exit() 
+0

啊好的,只是改变了下面的矩形(在这种情况下是直线)的颜色,然后用上面的透明度对图像进行了blits。这对你来说很好,但实际上这并不能回答这个问题。 – skrx

相关问题