Python pyGame图像缩放未按预期工作

Python pyGame图像缩放未按预期工作,python,image,pygame,transform,scale,Python,Image,Pygame,Transform,Scale,我是Python和pyGame新手,在缩放图像时遇到问题。 我想在pygame中缩放图像。 pygame文档声称 pygame.transform.scale() 应该扩展到一个新的解决方案。 但在我下面的示例中,它不起作用-它裁剪图像,而不是调整其大小!? 我做错了什么 #!/usr/bin/env python3 # coding: utf-8 import pygame from pygame.locals import * # Define some colors BLACK = (

我是Python和pyGame新手,在缩放图像时遇到问题。 我想在pygame中缩放图像。 pygame文档声称

pygame.transform.scale()

应该扩展到一个新的解决方案。 但在我下面的示例中,它不起作用-它裁剪图像,而不是调整其大小!? 我做错了什么

#!/usr/bin/env python3
# coding: utf-8

import pygame
from pygame.locals import *

# Define some colors
BLACK = (0, 0, 0)

pygame.init()

# Set the width and height of the screen [width, height]
screen = pygame.display.set_mode((1920, 1080))

pic = pygame.image.load('test.jpg').convert()
pic_position_and_size = pic.get_rect()

# Loop until the user clicks the close button.
done = False

# Clear event queue
pygame.event.clear()

# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
        if event.type == QUIT:
            done = True
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                done = True

    # background in black
    screen.fill(BLACK)

    # Copy image to screen:
    screen.blit(pic, pic_position_and_size)

    # Update the screen with what we've drawn.
    pygame.display.flip()
    pygame.display.update()

    pygame.time.delay(10)    # stop the program for 1/100 second

    # decreases size by 1 pixel in x and y axis
    pic_position_and_size = pic_position_and_size.inflate(-1, -1)

    # scales the image
    pic = pygame.transform.scale(pic, pic_position_and_size.size)

# Close the window and quit.
pygame.quit()

pygame.transform.scale()
对您的情况不太适用。如果将
曲面
缩小如此之小的量,算法只会裁剪最后一列和最后一行像素。如果您现在使用相同的
曲面反复重复此过程,您会看到奇怪的行为

更好的方法是保留原始
曲面
的副本,并将其用于创建缩放图像。此外,使用
smoothscale
代替
scale
也可能会产生更好的效果;如果你想用它,就看你了

以下是您的代码的“固定”版本:

#!/usr/bin/env python3
# coding: utf-8

import pygame
from pygame.locals import *

# Define some colors
BLACK = (0, 0, 0)

pygame.init()

# Set the width and height of the screen [width, height]
screen = pygame.display.set_mode((1920, 1080))

org_pic = pygame.image.load('test.jpg').convert()
pic_position_and_size = org_pic.get_rect()
pic = pygame.transform.scale(org_pic, pic_position_and_size.size)
# Loop until the user clicks the close button.
done = False

# Clear event queue
pygame.event.clear()

# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
        if event.type == QUIT:
            done = True
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                done = True

    # background in black
    screen.fill(BLACK)

    # Copy image to screen:
    screen.blit(pic, (0,0))

    # Update the screen with what we've drawn.
    pygame.display.flip()
    pygame.display.update()

    pygame.time.delay(10)    # stop the program for 1/100 second

    # decreases size by 1 pixel in x and y axis
    pic_position_and_size = pic_position_and_size.inflate(-1, -1)

    # scales the image
    pic = pygame.transform.smoothscale(org_pic, pic_position_and_size.size)

# Close the window and quit.
pygame.quit()

非常感谢您的快速回答!解决了我的问题!然而,我想知道为什么pygame文档中没有提到这一点。。。我花了好几个小时到处玩,我尝试了一切。。。