Antialiasing shapes in Pygame

kmaork picture kmaork · May 25, 2014 · Viewed 11.6k times · Source

I'm using Pygame to draw some things. The problem is that they have a lot of aliased edges:

Aliased concentric circles

I want to make them softer, like this:

Smooth concentric circles


My idea so far was to draw the shape in double size, and then use

pygame.transform.smoothscale(surf, (w/2, h/2))

to shrink it to the size I want.

Unfortunately if I'm drawing my shapes on a transparent surface, the smoothscale turns the edges that were touching transparent pixels black!

  • How do I smoothscale a picture without turning alpha-transparent pixels into black, or
  • How else do I antialias edges?

Answer

andreasdr picture andreasdr · Nov 6, 2014

In order to draw antialiased filled shapes with pygame, use the module gfxdraw and draw one antialiased outline and one regular filled shape. From http://www.pygame.org/docs/ref/gfxdraw.html:

To draw an anti aliased and filled shape, first use the aa* version of the >function, and then use the filled version.

Note that you need to import gfxdraw explicitly, i.e. from pygame import gfxdraw.