Losing image quality when resizing in pygame

4

I have some card images in size 140x190 and I'm using the pygame.trnasform.scale method to resize to 105x143 which is a proportional value. But it is losing quality, whereas the images of verses that are basically a background color and a white border do not lose. How do I resolve this?

Code within the class:

    self.frente = pygame.image.load("imagens/{}/carta ({}).png".format(naipe, valor)).convert()
    self.frente = pygame.transform.scale(self.frente, [98, 133])
    self.verso = pygame.image.load("imagens/versos/azul (1).png")
    self.verso = pygame.transform.scale(self.verso, [98, 133])
    
asked by anonymous 28.10.2017 / 16:06

1 answer

1

To avoid this loss of quality, you can use the pygame.transform.smoothscale() to resize the image with a special smooth filter.

In the example below, a image was used in the format PNG with transparency, where it was resized and displayed in 7 different scales 1.0 , 0.88 , 0.75 , 0.5 , 0.33 , 0.25 and 0.1 :

import pygame

running = True
verde_escuro = (0,128,0)

# Inicializa pygame
pygame.init();

# Inicializa janela com fundo verde escuro
screen = pygame.display.set_mode((1350, 480))
screen.fill(verde_escuro)

# Carrega uma imagem PNG com transparencia
img = pygame.image.load("carta.png").convert_alpha()

# Recupera as dimensoes da imagem
w, h = img.get_size()

# Escalas da imagem
scales = [ 1, 0.88, 0.75, 0.5, 0.33, 0.25, 0.1 ]

# Exibe a mesma imagem, em escalas diferentes, lado a lado.
posx = 0
for s in scales:
    redim = pygame.transform.smoothscale( img, (int(w*s), int(h*s)) )
    screen.blit( redim, (posx, 0) )
    posx += int(w*s)

# Loop principal de eventos
clock = pygame.time.Clock()
while running:
    clock.tick(10)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
    pygame.display.flip()

# Fim
pygame.exit()
sys.exit()

Output:

    
29.10.2017 / 19:23