[PyGame] image color - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] image color (/thread-2053.html) |
image color - Zman350x - Feb-14-2017 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 RE: image color - nilamo - Feb-14-2017 Once you've loaded the image as a surface, then you can access that surface's pixels with a PixelArray. The PixelArray class has a replace method, which replaces all instances of one color with another (so you don't need to check individual pixels yourself). When you create a PixelArray, it locks the underlying surface until it's garbage collected, so you should delete it when you're done (or use a function and return from it). http://www.pygame.org/docs/ref/pixelarray.html#pygame.PixelArray.replace So... 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 RE: image color - Zman350x - Feb-26-2017 could I replace the color with an alpha value? I want to change white to clear. RE: image color - nilamo - Feb-27-2017 Absolutely :) Color() takes for arguments, rgba. If you pass 0 as the final parameter, then it should be fully transparent (and the other three parameters wouldn't matter at that point). RE: image color - Zman350x - Feb-27-2017 I tried that. it ignored the final value RE: image color - nilamo - Feb-28-2017 What about Surface.set_colorkey()? https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey Wrote:Set the transparent colorkey So, if you set the colorkey to whatever color you want to be transparent, that... might... work? RE: image color - metulburr - Mar-03-2017 I made a script to show this example. Without the image you are using i can only use an example image [attachment=130] You can change which color is the transparent by change the from_ color. And it shows as white as the background is white before the image is drawn. 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() |