I\'m working on a short intro in PyGame, and I need to blur some static lines. After two days of searching, I can\'t find anything.
Does PyGame have a built-in method
There's no built-in way to blur a Surface
.
Workarounds are:
scale the Surface
with pygame.transform.smoothscale and then back to its original size (ugly)
iterating over each pixel, get the color of the neighbour pixels, calculate the average value for red, green, and blue, and set the color of that pixel (slow as hell if not using something like numpy
)
I recommend using the Python Imaging Library (PIL), since it's nice and fast.
Here's a simple and running example:
import pygame
from PIL import Image, ImageFilter
pygame.init()
screen = pygame.display.set_mode((512, 512))
clock = pygame.time.Clock()
compressed = 'eJztnV2LHEUUhv0J+w+cf5CAELxQGT+IqNGsicYQds3kc4nEZDBiYhKTNmaFEBeyagzEiIMiKCIOKiiSi0HEGxU2F+qFKIMXGhTMXBiE3LT9jt1Db6c/qnqq6pyqroaXZGene2fqOXVO1anT1WEY3hIy0OLcpnak2UhBrF6kQY76qfd04vNa1J/fS4r1TMwOjFcihYoE+zgXX9vbBCPFzLuKeVdpJf6b3hbouLfifm6KeZF63g6Ms4cfHjFgn9Y56nZpguI4TM26LC7MULeRq1r8fxxPzdjbAB3/AQO+IupSt5WLitp1yICtiALqtnJRDLh6/p6/iAbUbeWiGHD1/D1/z7+B/F/atys8EQThseUL4dGL74RH3v1oIrz2wpmzYXD42fB0Z6vn7xB/MD36xqVVvKt0fGn5avRvi7q9XFLEYq3R/r5nftyvZbjnqOftQBn/tsk+//zb70/LPtEoEmk+KPr77UidSEFKXbxOzZUbf7BXxD2rIRgY4j0T8+4Lfja8b5aaMTV/jeyz/qAf98G2gFoS3FvxdUc1P9sKV5+gmz/ivUKfr0MrJXYTxL9X9bcCat6m+SsY67mmHjVzU/xPHdhXq40ufvnVRAx4OW0DOvkjdyPaJmc+/jz8/pdhmHfgdfyeATeVYlHXpJO/aNz/8Jvvcrlnj69/+pmamWq1XeUv6vtF2SfH5Ss/UjObKPjgk1WxCj9LXmPoKv+Tx44Ktd+/N25I8f/ht9/JucNmf/97lPv5fr36V7j82WWZ63WI+Xd18MeaTtV3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mir0uUhdwz4Bfv4cfBH19/e6yRYwVwhiexgp7ueB7+v2KY4V7Zhfon7MNKte6j04x4/nJPUfe7dmpQ=='
size, image_mode, raw = (128, 128), 'RGBA', compressed.decode("base64").decode("zlib")
# create the original pygame surface
surf = pygame.image.fromstring(raw, size, image_mode)
# create a PIL image and blur it
pil_blured = Image.fromstring("RGBA", size, raw).filter(ImageFilter.GaussianBlur(radius=6))
# convert it back to a pygame surface
other = pygame.image.fromstring(pil_blured.tostring("raw", image_mode), size, image_mode)
pygame.time.set_timer(pygame.USEREVENT, 1000)
while True:
for e in pygame.event.get():
if e.type == pygame.USEREVENT: surf, other = other, surf
if e.type == pygame.QUIT: break
else:
screen.fill((255, 255, 255))
screen.blit(surf, (192, 192))
pygame.display.flip()
clock.tick(60)
continue
break
Hint: If you're on Windows and want to install PIL, download it from here and install it via
pip install Pillow-2.8.2-cp27-none-win32.whl
or whatever version you downloaded (installing PIL on Windows can be a great PITA otherwise).