4
\$\begingroup\$

I'm having a strange issue that I cannot seem to remedy. I am doing some prototyping with Pygame on a desktop running windows and a laptop running OS X. Both are running python v2.7.3 (installed via homebrew for the Macbook) and pygame v1.9.1.

For transparency, I have been using set_colorkey with a transparency color of (255, 0, 255). Here is the applicable code:

transColor = pygame.Color(255, 0, 255)
image = pygame.image.load(playerPath + "idle.png").convert()
image.set_colorkey(transColor)

This works flawlessly on my windows machine. On my laptop, it does not work. It just shows the hideous magenta color. Here's the strange part. If I change the transColor to (0, 0, 0), all black pixels in my images are transparent. Has anyone run into this issue before?

\$\endgroup\$
3
  • \$\begingroup\$ Sounds like a premultiplied alpha issue. \$\endgroup\$
    – Tetrad
    Jan 22, 2013 at 23:25
  • \$\begingroup\$ have you tried convert_alpha() instead of convert()? I thought that's the recommended one to use for surfaces with transparency. \$\endgroup\$ Jan 23, 2013 at 10:02
  • \$\begingroup\$ I tried convert_alpha() and it yielded the same result. I ended up just using PNG transparency rather than the colorkey \$\endgroup\$ Jan 24, 2013 at 15:38

3 Answers 3

1
\$\begingroup\$

Interesting. My guess is that the Convert call is changing the image into a form that does not have the 255,0,255 colour in it any more. You may need to read a pixel from the converted image which you know to be the transparent colour, and then set that as the colour key.

eg.:

image = pygame.image.load(playerPath + "idle.png").convert()
transColor = image.get_at((0,0))
image.set_colorkey(transColor)
\$\endgroup\$
2
  • \$\begingroup\$ This did work (grabbing the color at 0,0) but there were some instances that I did not want to use that color as a transparency. \$\endgroup\$ Jan 24, 2013 at 15:37
  • \$\begingroup\$ An alternative might be to have a 1x1 image of just the transparent colour, convert that, and extract the key colour from that image. The format should be the same across all images that are converted to the screen's optimal format. \$\endgroup\$
    – Kylotan
    Jan 24, 2013 at 16:10
1
\$\begingroup\$

After digging around a bit more, I found that only a handful of the images weren't displaying the transparently properly (namely the background images - which made it difficult to see that the foreground images were working). As I'm using PNGs, I know that a lot of people have had issues using set_colorkey() when there are alpha channels already present. Instead of continuing to troubleshoot this annoying bug, I opted to just drop the colorkey stuff from my code and use the built-in PNG transparency.

I guess that's not really an answer... but it's a resolution!

\$\endgroup\$
0
\$\begingroup\$

just try:

transColor = (255, 0, 255)
image = pygame.image.load(playerPath + "idle.bmp")
image.set_colorkey(transColor)

bmp format without convert

it works!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .