[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] Font antialiasing
IMHO, when it comes to rendering pretty text, good antialiasing is of utmost
importance. Therefore (nearly) the first thing I wrote when I started using
pygame was a simple antialiasing routine, because the standard one just
doesn't cut it.
Since I wrote this before the contest started i think it's only fair to make
it available to everybody. The function requires NumPy (I had a version
without it, but it took FOREVER to render). Did anybody say you couldn't
write cryptic code in Python ;) ?
alphaarray=pygame.surfarray.pixels_alpha(self.surface)
newalpha=zeros(add(alphaarray.shape,(2,2)))
newalpha[1:-1,1:-1]=alphaarray[:]
newalpha[1:-1,1:-1]=newalpha[1:-1,1:-1] *
(newalpha[:-2,1:-1] + newalpha[2:,1:-1] + newalpha[1:-1,:-2]
+ newalpha[1:-1,2:]) / 1020
alphaarray[:]=newalpha[1:-1,1:-1].astype(UnsignedInt8)
I personally think this is one of the fastest ways to do it in "pure" Python
(and I actually profiled quite some versions), but feel free to prove that
it can be done faster!
I included an image demonstrating the effect since it's only 26kB, I hope
you don't mind. The different "levels" of AA are just the above applied
multiple times.
Hope this helps some of you!
- Peter Thoman aka Dante
PS: This method is part of my class AccurateText, which itself is the base
for different stuff like shadowed, glowing or textured text -- just to clear
up the text in the image. Also note that I only alter the alpha channel,
making it easy to provide textures/gradients/etc RGB values.
antialias.gif