Feb-14-2017, 04:36 PM
I have an image of a white robot. How can I change the color white of that image to another color, like red or blue or green or something?
Happy Valentine's
Happy Valentine's

surface = load_image() # ...whatever you're already doing pixels = PixelArray(surface) pixels.replace(Color(255, 255, 255, 255), Color(0, 0, 255, 255)) del pixels
https://www.pygame.org/docs/ref/surface....t_colorkey Wrote:Set the transparent colorkey
set_colorkey(Color, flags=0) -> None
set_colorkey(None) -> None
Set the current color key for the Surface. When blitting this Surface onto a destination, any pixels that have the same color as the colorkey will be transparent. The color can be an RGB color or a mapped color integer. If None is passed, the colorkey will be unset.
The colorkey will be ignored if the Surface is formatted to use per pixel alpha values. The colorkey can be mixed with the full Surface alpha value.
The optional flags argument can be set to pygame.RLEACCEL to provide better performance on non accelerated displays. An RLEACCEL Surface will be slower to modify, but quicker to blit as a source.
import pygame as pg pg.init() COLORKEY = (0,0,0) #black RED = (221,0,0) ORANGE = (254,98,98) YELLOW = (254,246,0) GREEN = (0,187,0) BLUE = (0,155,254) INDIGO = (0,0,131) VIOLET = (48,0,155) def swap_color(surf, from_, to_): arr = pg.PixelArray(surf) arr.replace(from_,to_) del arr class Rainbow: def __init__(self, screen_rect): self.screen_rect = screen_rect self.image = pg.image.load('rainbow.png').convert() self.rect = self.image.get_rect(center=self.screen_rect.center) swap_color(self.image, (0,187,0), COLORKEY) self.image.set_colorkey(COLORKEY) def draw(self, surf): surf.blit(self.image, self.rect) screen = pg.display.set_mode((1920,1080)) screen_rect = screen.get_rect() player = Rainbow(screen_rect) done = False while not done: screen.fill((255,255,255)) for event in pg.event.get(): if event.type == pg.QUIT: done = True player.draw(screen) pg.display.update()