我有矩形:
rect = pygame.Rect(10, 20, 100, 200)
和图片:
img = pygame.image.load('ace_of_hearts.jpg')
如何更改较大(或较小)的图像以适合矩形(图像的初始比例)?
答案 0 :(得分:0)
你可以这样做:
rect = pygame.Rect(10, 20, 100, 200)
img = pygame.image.load('ace_of_hearts.jpg')
img = pygame.transform.scale(img, rect.size)
# rect.size returns (100, 200)
希望这有帮助!
答案 1 :(得分:0)
def resizeInto(surface, rect):
surSize = list(surface.get_size())
rectSize = rect.size
for i in (0, 1):
if surSize[i] > rectSize[i]:
surSize[1 - i] = 1. * surSize[1 - i] / surSize[i] * rectSize[i]
surSize[i] = rectSize[i]
if surSize != surface.get_size():
rounded = [int(round(s)) for s in surSize]
surface = pygame.transform.smoothscale(surface, rounded)
return surface