Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python - How do i properly rescale an image on PyGame without it being badly cropped?

I'm trying to rescale an image on pygame but it instead crops the image, I'm just looking to change the pixel width and height of the image.

I used pygame.transform.scale and used 1900, 600 as the parameters, the original image is a 1920x1080

mainmenu = pygame.image.load("Main_Menu.png")
mainmenu = pygame.transform.scale(mainmenu, (1900,600))
screen.blit(background, (0, 0))

I expected the image to be rescaled to a 1900x600 image but it instead output a badly cropped image in the 1900x600 format.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You've to use pygame.transform.smoothscale() rather than pygame.transform.scale().
Use convert() or convert_alpha() to convert the surface to a surface with a with proper pixel format

mainmenu = pygame.image.load("Main_Menu.png")
mainmenu = pygame.transform.smoothscale(mainmenu.convert_alpha(), (1900,600)) 
screen.blit(background, (0, 0))

While scale() does a scaling operation, which is as fast as possible, smoothscale() scales smoothly and calculates area averages of the colors which cover the pixels.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...