Losing image quality when resizing in pygame
I have some card images in size 140x190 and I am using the pygame.trnasform.scale
Method to resize to 105x143 which is a proportional value. But it is losing quality, while images of verses that are basically a background color and a white border do not lose. How to solve this?
Code inside 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])
4
Author: Matheus Silva Pinto, 2017-10-28
1 answers
To avoid this loss of quality, you can use the method pygame.transform.smoothscale()
to resize the image with a special filter of smooth .
In the example below we used a image in the format PNG
with transparency, where it was resized and displayed in 7 different scales1.0
,0.88
,0.75
,0.5
,0.33
,0.25
e 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:
1
Author: Lacobus, 2017-10-29 19:37:17