Losing image quality while resizing in pygame

Asked

Viewed 735 times

4

I have some pictures of cards in size 140x190 and am using the method pygame.trnasform.scale to resize to 105x143 which is a proportional value. But it is losing quality, while the images of the verses that are basically a background color and a white border do not lose. How to solve 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])

1 answer

1


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

In the example below we used a image in format PNG with transparency, where it was resized and displayed on 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()

Exit:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.