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

Re: [SPAM: 6.000] Re: [pygame] How do I make a image transparent?



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

DR0ID wrote:
> Perhaps you should write a convert script that removes the alphachannel
> and replaces it with a colorkey of your choice. The main problem will be
> what do you do with half transparent pixels? e.g. pixel where the alpha
> value is between 0 and 255.

I have done this; in case anyone else wants it, it's a line of
ImageMagick code, as follows:

convert -compose Over -fill "rgb(255, 0, 255)" \
  -draw "color 0,0 reset" $1 -draw "image Over 0,0 0,0 '$1'" \
  +matte $2

(where $1 is the name of the input file and $2 the name of the output
file). (255, 0, 255) is magenta and doesn't often come up in the game
I'm working on. As for half-transparent pixels, I tell my artist not
to make them :) but maybe subtracting the alpha would be a good
solution too. (I'm not sure how to do that in ImageMagick though.)

However, because it potentially could collide with a color in an image
in the game, I save the colorkey to a separate file. Actually, for a
given file foo.png I check for a file foo.inf which contains a
statement like:

 colorkey = True  # this means take the colorkey from the top-left
pixel, as Greg Ewing suggests

 colorkey = (x, y)   # colorkey is a pixel at (x, y)

 colorkey = (r, g, b)   # colorkey is (r, g, b)

 colorkey = False   # no colorkey; this is the default

If no foo.inf file is found, complain, and assume colorkey = False.
Even in this engine, per-pixel alpha may be needed for some graphics.

This system was grown, not designed, and if I were to redo it, I think
I would make colorkey = (255, 0, 255) the default, as you suggest,
because almost all of the images use that as the colorkey, with
colorkey = False available in case per-pixel alpha is needed. But as
it is, I just have a ton of .inf files floating around my graphics
directory.

> From the set_alpha() documentation:
> "If the Surface contains per pixel alphas, setting the alpha value to
> None will disable the per pixel transparency."
> 
> But I dont know what dolor this pixels will have afterwards.

My guess would be that it would strip the alpha channel entirely:
(255, 0, 0, 127) would become (255, 0, 0).

> Greg Ewing schrieb:
>> Probably you could just throw away the alpha channel
>> and use the colour of the top left pixel as the
>> colorkey.

This really doesn't work if you have a rectangular image with a hole
in the center or something like that. It's tricky to characterize what
kinds of images you'll have in your game before you have them, and if
you want reasonably generic image loading code, you have to make some
guesses.

Ethan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG6h4BhRlgoLPrRPwRAhMSAKCLYSa/Pq6U+p1u9xCSk9vhX2nXNwCdF0Fa
QL/KwXKZ4A/9mHVmb9Fx094=
=olAO
-----END PGP SIGNATURE-----