'Blurred vignette in pygame

I'm trying to make a pygame surface become blurred at the edges for a visual effect. The centre of the surface would remain unblurred. I found a way of blurring a pygame surface with transform.smoothscale, but it seems that this only works for a whole surface (Blurring in PyGame)

I've managed to make a script that blurs only the edges of the screen, but the transition to the blur is sharp and not smooth. Is having a smooth transition between blurred and not blurred. Is this something that is possible? Thanks



Solution 1:[1]

Well, it's been a few days and I finally managed to work out a solution:

def blur_surface(surf, amount, iterations):
for i in range(iterations):
    surf = pygame.transform.smoothscale(surf, (display_width / amount, 
    display_height / amount))
    surf = pygame.transform.smoothscale(surf, (display_width, display_height))
return surf

blursurf = display.copy()
blursurf = blur_surface(blursurf, 2, 3)
blursurf.blit(blur_image, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
display.blit(blursurf, (0, 0))

'blur_image' is an image of a circle that is black in the centre, and slowly fades to transparent at the edges (like this).

Here's how it works:

  1. The surface that we want to blur is copied.
  2. The entire copy is blurred.
  3. The image of a faded circle is blitted onto the copy, but with a flag that makes pygame cut a hole out of the surface instead.
  4. The new surface is blitted ontop of the original.

I hope this helps anyone who ends up with the same problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Smarf