如何在Pygame中将图像缩放到屏幕大小

时间:2013-11-15 13:31:11

标签: python image background pygame scaling

我想知道如何将pygame项目中图像的大小缩放到屏幕的分辨率。例如,设想以下场景假设当前窗口显示模式;我认为全屏将是相同的:

  • 我有1600x900背景图片,当然在1600x900窗口中显示

  • 1280x720窗口中,我显然可以将此图片缩放为1280x720

  • 但是,如果我需要添加,请说300x300 px x,y 1440,860图片1600x900图片(示例尺寸),其大小适合原始1600x900背景?当然对于{{1}}我当然可以原生使用图像,但是较小/较大的窗口大小呢?

基本上,如何将图像缩放到窗口大小然后相应地定位?我想必须有一个非常简单的自动化方法,但是现在我无法理解它,坦率地说没有时间去搜索它......

提前致谢, Ilmiont

4 个答案:

答案 0 :(得分:53)

您可以使用pygame.transform.scale缩放图片:

import pygame
picture = pygame.image.load(filename)
picture = pygame.transform.scale(picture, (1280, 720))

然后,您可以使用

获取picture的边界矩形
rect = picture.get_rect()

并使用

移动图片
rect = rect.move((x, y))
screen.blit(picture, rect)

其中screen设置为

screen = pygame.display.set_mode((1600, 900))

要让您的小部件适应各种屏幕尺寸, 你可以make the display resizable

import os
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500, 500), HWSURFACE | DOUBLEBUF | RESIZABLE)
pic = pygame.image.load("image.png")
screen.blit(pygame.transform.scale(pic, (500, 500)), (0, 0))
pygame.display.flip()
while True:
    pygame.event.pump()
    event = pygame.event.wait()
    if event.type == QUIT:
        pygame.display.quit()
    elif event.type == VIDEORESIZE:
        screen = pygame.display.set_mode(
            event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE)
        screen.blit(pygame.transform.scale(pic, event.dict['size']), (0, 0))
        pygame.display.flip()

答案 1 :(得分:6)

如果您将1600x900缩放为1280x720,则

scale_x = 1280.0/1600
scale_y = 720.0/900

您可以使用它来查找按钮大小和按钮位置

button_width  = 300 * scale_x
button_height = 300 * scale_y

button_x = 1440 * scale_x
button_y = 860  * scale_y

如果您将1280x720扩展到1600x900,则

scale_x = 1600.0/1280
scale_y = 900.0/720

和休息是一样的。


我将.0添加到值float - 否则scale_xscale_y将四舍五入为integer - 在此示例中为0 }(零)(Python 2.x)

答案 2 :(得分:1)

我不知道你是不是这个意思,但这是如何在不丢失图像的宽度和高度之间的纵横比的情况下,尽可能将图像缩放到屏幕的大小

row = pygame.image.load(f"{image}")
x, y = row.get_size()
rx = 1000 / x
ry = 600 / y
print(rx)
print(ry)
ratio = rx if rx < ry else ry
row = pygame.transform.scale(row, (int(x*rx), int(y*rx)))

答案 3 :(得分:0)

这是一个允许将图像缩放到屏幕以保持纵横比并且永远不会超出屏幕的方法。

screen_resolution = (1920, 1080)
image_path = '/path/to/image.png'
center_image = True
image = pygame.image.load(image_path)

screen_w, screen_h = screen_resolution
image_w, image_h = image.get_size()

screen_aspect_ratio = screen_w / screen_h
photo_aspect_ratio = image_w / image_h

if screen_aspect_ratio < photo_aspect_ratio:  # Width is binding
    new_image_w = screen_w
    new_image_h = int(new_image_w / photo_aspect_ratio)
    image = pygame.transform.scale(image, (new_image_w, new_image_h))
    image_x = 0
    image_y = (screen_h - new_image_h) // 2 if center_image else 0

elif screen_aspect_ratio > photo_aspect_ratio:  # Height is binding
    new_image_h = screen_h
    new_image_w = int(new_image_h * photo_aspect_ratio)
    image = pygame.transform.scale(image, (new_image_w, new_image_h))
    image_x = (screen_w - new_image_w) // 2 if center_image else 0
    image_y = 0

else:  # Images have the same aspect ratio
    image = pygame.transform.scale(image, (screen_w, screen_h))
    image_x = 0
    image_y = 0

display.blit(image, (image_x, image_y))
相关问题