Posts Tagged ‘ blur

How can I blur a PyGame Surface?

From over on my PyGame Wiki…

There doesn’t seem to be a built in way in PyGame to ‘blur’ a surface, but you can fake it bay scaling the surface twice: Scale it small once, then scale it back to normal size.

def blurSurf(surface, amt):
    """
    Blur the given surface by the given 'amount'.  Only values 1 and greater
    are valid.  Value 1 = no blur.
    """
    if amt < 1.0:
        raise ValueError("Arg 'amt' must be greater than 1.0, passed in value is %s"%amt)
    scale = 1.0/float(amt)
    surf_size = surface.get_size()
    scale_size = (int(surf_size[0]*scale), int(surf_size[1]*scale))
    surf = pygame.transform.smoothscale(surface, scale_size)
    surf = pygame.transform.smoothscale(surf, surf_size)
    return surf

You can also use pygame.transform.rotozoom() in place of smoothscale. However, they have different effects: smoothscale appears to blur (and push) the pixels ‘outward’ based on the center of the surface, while rotozoom blurs (and pushes) them outward based on the top left corner of the image (0,0), effectively making them translate down and to the right in the process.
http://www.pygame.org/docs/ref/transform.html#pygame.transform.smoothscale
http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotozoom

Other methods I haven’t investigated would involve running a convolve routine on the surface pixels.  As is, I’m not sure how could the performance would be though.

I’d be interested to know what methods others have come up with to solve this problem.  My searching hasn’t come up with much.