首页 > 解决方案 > 如何在 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 中正确实现它。

任何事情将不胜感激,谢谢!

标签: pythonpygamevector-graphicspygame-surface

解决方案


使用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()

推荐阅读