Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 869
kmaork Avatar asked May 25 '14 07:05

kmaork


People also ask

How do I add anti-aliasing to pygame?

To make Pygame's text use anti-aliasing, just pass True for the second parameter of the render() method. The pygame. draw. aaline() and pygame.

What is Gfxdraw?

gfxdraw allows anti-aliasing for all shapes, where draw only adds antialiasing to lines. I use gfxdraw by default. It has been marked 'experimental' for a long time now, but I've not had any issues. Follow this answer to receive notifications.


1 Answers

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.

like image 129
andreasdr Avatar answered Sep 24 '22 14:09

andreasdr