Skip to content
Advertisement

Antialiasing shapes in Pygame

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?

Advertisement

Answer

In order to draw antialiased filled shapes with pygame, use the module gfxdraw and draw one antialiased outline and one regular filled shape.

From https://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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement