Search code examples
pythonpygamevector-graphicspygame-surface

How can you draw more detailed/smoother images in pygame?


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:

Here is the image

and here is how it looks in pygame

enter image description here

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!


Solution

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