如何在 pygame 中绘制更详细/更平滑的图像? [英] How can you draw more detailed/smoother images in pygame?

查看:135
本文介绍了如何在 pygame 中绘制更详细/更平滑的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试进入矢量风格的艺术世界,最近我尝试使用 .blit() 方法对矢量图像进行 blitting,但是当我对它进行 blit 时,它显示为像素化.

图片如下:

这是它在 pygame 中的样子

代码如下:

导入pygame屏幕 = pygame.display.set_mode((500,500))img = pygame.image.load("C:/Users/socia/Downloads/9nA8s.png")img = pygame.transform.scale(img, (500,500))正在运行 = 真运行时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:正在运行 = 错误screen.blit(img, (0,0))pygame.display.update()

我如何绘制与第一个提到的相似的图像,以及如何在 pygame 中正确实现它.

不胜感激,谢谢!

解决方案

使用

导入pygame从 svg 导入解析器,光栅化器def load_svg(filename, scale=None, size=None, clip_from=None, fit_to=None, foramt='RGBA'):svg = Parser.parse_file(文件名)scale = min((fit_to[0]/svg.width, fit_to[1]/svg.height)if fit_to else ([scale if scale else 1] * 2))宽度,高度 = 大小,如果是其他大小(svg.width, svg.height)冲浪尺寸 = 圆形(宽度 * 比例),圆形(高度 * 比例)buffer = Rasterizer().rasterize(svg, *surf_size, scale, *(clip_from if clip_from else 0, 0))返回 pygame.image.frombuffer(buffer, surf_size, foramt)pygame.init()窗口 = pygame.display.set_mode((300, 300))时钟 = pygame.time.Clock()pygameSurface = load_svg('Ice.svg', fit_to = (window.get_width()*3//4, window.get_height()))运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window.fill((127, 127, 127))window.blit(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))pygame.display.flip()pygame.quit()出口()

I have been trying to get into the vector-styled art world and recently I've tried blitting a vector image using the .blit() method but when I do blit it, it comes out as pixelated.

Here is the image:

and here is how it looks in pygame

with the code of:

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

How could I draw similar images like the first one mentioned and how could I properly implement it in pygame.

Anything would be greatly appreciated, thanks!

解决方案

Use pygame.transform.smoothscale instead of pygame.transform.scale:

img = pygame.transform.scale(img, (500,500))

img = pygame.transform.smoothscale(img, (500,500))

While pygame.transform.scale performs a fast scaling with the nearest pixel, pygame.transform.smoothscale scales a surface smoothly to any size with interpolation of the pixels.


For an even better result, you may want to switch to a vector graphic format such as SVG (Scalable Vector Graphics).
See the answers to the question SVG rendering in a PyGame application a nd the following minimal example:

import pygame
from svg import Parser, Rasterizer

def load_svg(filename, scale=None, size=None, clip_from=None, fit_to=None, foramt='RGBA'):
    svg = Parser.parse_file(filename)
    scale = min((fit_to[0] / svg.width, fit_to[1] / svg.height)
                if fit_to else ([scale if scale else 1] * 2))
    width, height = size if size else (svg.width, svg.height)
    surf_size = round(width * scale), round(height * scale)
    buffer = Rasterizer().rasterize(svg, *surf_size, scale, *(clip_from if clip_from else 0, 0))
    return  pygame.image.frombuffer(buffer, surf_size, foramt)

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

pygameSurface = load_svg('Ice.svg', fit_to = (window.get_width()*3//4, window.get_height()))

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(pygameSurface, pygameSurface.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

这篇关于如何在 pygame 中绘制更详细/更平滑的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆