How can I change an image's color in Pygame? I have a blue hexagon png file and is there any simple way to load it and change it to red color or something?
Asked
Active
Viewed 3,398 times
1 Answers
1
If you want to fill the entire image with a single color but preserve the transparency, you can utilize two nested for
loops and the pygame.Surface.set_at
method to change every pixel of the surface.
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
img = pg.Surface((150, 150), pg.SRCALPHA)
pg.draw.polygon(img, (0, 100, 200), ((75, 0), (150, 75), (75, 150), (0, 75)))
def set_color(img, color):
for x in range(img.get_width()):
for y in range(img.get_height()):
color.a = img.get_at((x, y)).a # Preserve the alpha value.
img.set_at((x, y), color) # Set the color of the pixel.
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_j:
set_color(img, pg.Color(255, 0, 0))
elif event.key == pg.K_h:
set_color(img, pg.Color(0, 100, 200))
screen.fill(BG_COLOR)
screen.blit(img, (200, 200))
pg.display.flip()
clock.tick(60)
If you want to tint a surface, take a look at this post: https://stackoverflow.com/a/49017847/6220679

skrx
- 19,980
- 5
- 34
- 48
-
Thanks. Although the first way didn't work, the second way worked perfectly for me. – jwoojin9 Dec 24 '18 at 13:36