【问题标题】:How can you draw more detailed/smoother images in pygame?如何在 pygame 中绘制更详细/更平滑的图像?
【发布时间】:2021-04-06 02:53:16
【问题描述】:

我一直在尝试进入矢量风格的艺术世界,最近我尝试使用 .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 中正确实现它。

任何东西都将不胜感激,谢谢!

【问题讨论】:

标签: python pygame vector-graphics pygame-surface


【解决方案1】:

使用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 (Scalable Vector Graphics)
请参阅问题SVG rendering in a PyGame application 的答案以及以下最小示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2014-10-11
    • 2021-11-26
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多