python - 如何在 pygame 中绘制更详细/更平滑的图像?
问题描述
我一直在尝试进入矢量风格的艺术世界,最近我尝试使用 .blit() 方法对矢量图像进行 blitting,但是当我对它进行 blit 时,它会显示为像素化。
这是图像:
这是它在 pygame 中的样子
使用以下代码:
import pygame
screen = pygame.display.set_mode((500,500))
img = pygame.image.load("C:/Users/socia/Downloads/9nA8s.png")
img = pygame.transform.scale(img, (500,500))
isrunning = True
while isrunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
isrunning = False
screen.blit(img, (0,0))
pygame.display.update()
我怎么能像提到的第一个一样绘制类似的图像,以及如何在 pygame.js 中正确实现它。
任何事情将不胜感激,谢谢!
解决方案
使用pygame.transform.smoothscale
代替pygame.transform.scale
:
img = pygame.transform.scale(img, (500,500))
img = pygame.transform.smoothscale(img, (500,500))
在使用最近的像素pygame.transform.scale
执行快速缩放时,通过像素插值将曲面平滑地缩放到任何大小。pygame.transform.smoothscale
为了获得更好的结果,您可能需要切换到矢量图形格式,例如SVG(可缩放矢量图形)。
请参阅问题SVG 渲染在 PyGame 应用程序中的答案和以下最小示例:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
pygame_surface = pygame.image.load('Ice.svg')
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill((127, 127, 127))
window.blit(pygame_surface, pygame_surface.get_rect(center = window.get_rect().center))
pygame.display.flip()
pygame.quit()
exit()
推荐阅读
- python - 在没有上下文信息的情况下从元组访问对象的值
- python - 如何将 curl --data-binary 转换为 python 请求?
- unicode - unicode 字符/表情符号可以作为手机密码吗?
- python - 在python中,如何从数组元素中获取部分字符串
- python - 如何从 hive 查询/pyspark 中获取以 unix 时间戳给出的最大日期
- r - R Shiny DataTable如何防止包含超链接的列中的行选择/取消选择
- weka - 如何应用 PCA 但保留原始属性?
- r - 如何为因子 x 轴构建带有渐变填充和顶部点/线的 geom_tile/rect 框?
- python - 如何在 tkinter 中设置 PanedWindow 内的默认帧大小?
- android - 使用 Progressive Web App,我可以检索手机的电话号码吗