[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How to replace a color in an image?



Thanks very much for both replies! Pixelarray looks like it will do exactly what I need at the moment.

Nirav: I'm also interested in the transform.threshold method, but am a bit confused as to how to implement it. 

Let's say I have:
screen = pygame.display.set_mode(SCREEN_SIZE)
map_image = pygame.image.load('map_image.png').convert_alpha()

And then in my main game loop, I render with:
screen.blit(map_image, (0, 0))
pygame.display.update()


How/where exactly do I implement transform.threshold? I think part of what's confusing me is having the two surface parameters, "destsurface," and "surface"...


Thanks again!

Jordan


On Mon, Mar 15, 2010 at 10:53 AM, Nirav Patel <olpc@xxxxxxxxxxxxxx> wrote:
A very quick way of doing it is just to use transform.threshold [0].
Something along the lines of:

yellow = (255, 255, 0) # or whatever yellow color you want
threshold = (10, 10, 10) # or whatever threshold works
blue = (0, 0, 255)
pygame.transform.threshold(destsurface, surface, yellow, thresh, blue,
1, None, True)

[0] http://www.pygame.org/docs/ref/transform.html#pygame.transform.threshold

On Sun, Mar 14, 2010 at 5:34 PM, Ian Mallett <geometrian@xxxxxxxxx> wrote:
> Hi,
>
> You could replace the pixels of the given color using surfarray or
> pixelarray before blitting.  You could also make it an 8 bit surface and
> adjust the palette.  You could also just do a manual loop though all the
> pixels with .get_at(), .set_at().  Personally, I'd go with surfarray or
> pixelarray--it's fast, and doesn't suffer from bit-depth loss like
> palettes.
>
> There may be better ways, contingent on what exactly you're doing.  You may
> be able to adjust your algorithm so that this situation doesn't happen.  A
> more complete description of what you're doing?
>
> Ian
>