我正在 PyGame 中做一个简短的介绍,我需要模糊一些静态线。找了两天了,没有找到任何东西。
PyGame 是否有内置方法来模糊形状或表面?我需要手动完成吗?解决这个问题的最佳方法是什么?
澄清一下,我不是在寻找运动模糊 - 我只需要一个简单的就地模糊,几乎就像发光效果。
2023 更新: 如果您使用 pygame-ce,函数
pygame.transform.box_blur
和 pygame.transform.gaussian_blur
已在 2.2.0 版本中添加。
没有内置方法可以模糊
Surface
。
解决方法是:
用
Surface
缩放
pygame.transform.smoothscale
,然后恢复到原来的大小(丑陋)
迭代每个像素,获取相邻像素的颜色,计算红色、绿色和蓝色的平均值,并设置该像素的颜色(如果不使用
numpy
之类的东西,速度会很慢)
我建议使用Python Imaging Library (PIL),因为它很好而且速度很快。
这是一个简单且运行的示例:
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/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8Zog79vKzel3XJv4SGsR9Uif3wPU+bzN/fOaITzdmpYr7sGncbeYf28BMzGw4BfNeU/y8a/zTihiujX0CeA4K1I/tZfaI5TU7nr+X5+/l+Xt5/l6ev9cU/GcZcPX8aW2gx4CtiKy+146zFvnHgVEkZ+quOCpq39Yig/1gcgT/1LjcLKEdtJnEBNhim7o9mir0uUhdwz4Bfv4cfBH19/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
提示:如果您使用的是 Windows 并且想要安装 PIL,请从 here 下载并通过
安装pip install Pillow-2.8.2-cp27-none-win32.whl
或您下载的任何版本(在 Windows 上安装 PIL 可能是一个很棒的 PITA)。
@sloth 有 python 3 版本的代码:
import sys
import base64
import zlib
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+zgXX9vbBCPFzLuKeVdpJf6b3hbouLfifm6KeZF63g6Ms4cf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EDagZ2szftjUBz1+dLBvne/4KJXK/mCXy/OvEfMH7hSyQ55/j1xHXMa5DTbjDfR9ie28/Bf+88Rz2fcGaDvK6NtaBVCigZsiFv+XzuLqyeg1YJX+HfLqMWtQMufBnwMK0htT8OPFv4DP+fP1HOv67M6cTlZX3/OniP77npzk+YEDNjht/CPu8NmQc2KZmx5F/Q2zAib6vi38DbMD6uK+b/9gG9szbem9HmQJqZrbwT+RQXnBAzctG/hDquyyfG2CPF5Z7eNvAH8L80NIcgZPsTfNPjwssus/PWfYx/75p/umYUPVceIwf6zw7XpF6LrOP+QdU/Cf+IJorYowIzonwM15P+4xj59+8Zog79vKzel3XJv4SGsR9Uif3wPU+bzN/fOaITzdmpYr7sGncbeYf28BMzGw4BfNeU/y8a/zTihiujX0CeA4K1I/tZfaI5TU7nr+X5+/l+Xt5/l6ev9cU/GcZcPX8aW2gx4CtiKy+146zFvnHgVEkZ+quOCpq39Yig/1gcgT/1LjcLKEdtJnEBNhim7o9mir0uUhdwz4Bfv4cfBH19/e6yRYwVwhiexgp7ueB7+v2KY4V7Zhfon7MNKte6j04x4/nJPUfe7dmpQ=='
zlib_data = base64.b64decode(compressed)
size, image_mode, raw = (128, 128), 'RGBA', zlib.decompress(zlib_data)
# create the original pygame surface
surf = pygame.image.fromstring(raw, size, image_mode)
# create a PIL image and blur it
pil_blured = Image.frombytes("RGBA", size, raw).filter(ImageFilter.GaussianBlur(radius=6))
# convert it back to a pygame surface
other = pygame.image.fromstring(pil_blured.tobytes("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:
pygame.quit()
sys.exit()
break
else:
screen.fill((255, 255, 255))
screen.blit(surf, (192, 192))
pygame.display.flip()
clock.tick(60)
continue
break