如何将图像缩放 window 大小?
How to scale an image by window size?
我有一个正方形,不可调整大小,pygame window,它的大小是根据显示器大小计算的。我有一些非正方形的图像,我希望按 window 大小缩放创建这些图像。我希望它通过我的 SCREEN_SIZE 变量进行缩放。这是我的代码,它将图像放在屏幕中间,但是如果您更改分辨率,图像将保持相同大小:
import pygame
pygame.init()
# Sets screen size by using your monitors resolution
SCREEN_SIZE = pygame.display.Info().current_h/1.65
screen = pygame.display.set_mode((int(SCREEN_SIZE), int(SCREEN_SIZE)))
gameRunning = True
""" This is the part I need help with. When I run my program the image is the correct
size, but when I change my resolution the image stays the same size and the window's
size changes. I need the image to proportionally scale with the window. """
imgSize = 0.2
img = pygame.image.load(r"location of an image on your PC")
img = pygame.transform.smoothscale(img, (int(img.get_width() * imgSize), int(img.get_height() * imgSize)))
rect = img.get_rect()
rect.center = (screen.get_width()/2, screen.get_height()/2)
while gameRunning == True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
pygame.quit()
break
screen.fill((255,255,255))
screen.blit(img, rect)
要使代码正常工作,您需要在“img”变量的图像位置中进行子操作,并弄乱 imgSize 变量。
比较宽高比。按最小比例缩放图像。这甚至适用于非正方形 window 分辨率。
ratio_x = screen.get_width() / img.get_width()
ratio_y = screen.get_height() / img.get_height()
scale = min(ratio_x, ratio_y)
img = pygame.transform.smoothscale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
img = pygame.image.load(r"parrot1.png")
scale = min(screen.get_width() / img.get_width(), screen.get_height() / img.get_height())
img = pygame.transform.smoothscale(img,
(round(img.get_width() * scale), round(img.get_height() * scale)))
rect = img.get_rect(center = screen.get_rect().center)
gameRunning = True
while gameRunning == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
screen.fill((127,127,127))
screen.blit(img, rect)
pygame.display.flip()
pygame.quit()
exit()
我有一个正方形,不可调整大小,pygame window,它的大小是根据显示器大小计算的。我有一些非正方形的图像,我希望按 window 大小缩放创建这些图像。我希望它通过我的 SCREEN_SIZE 变量进行缩放。这是我的代码,它将图像放在屏幕中间,但是如果您更改分辨率,图像将保持相同大小:
import pygame
pygame.init()
# Sets screen size by using your monitors resolution
SCREEN_SIZE = pygame.display.Info().current_h/1.65
screen = pygame.display.set_mode((int(SCREEN_SIZE), int(SCREEN_SIZE)))
gameRunning = True
""" This is the part I need help with. When I run my program the image is the correct
size, but when I change my resolution the image stays the same size and the window's
size changes. I need the image to proportionally scale with the window. """
imgSize = 0.2
img = pygame.image.load(r"location of an image on your PC")
img = pygame.transform.smoothscale(img, (int(img.get_width() * imgSize), int(img.get_height() * imgSize)))
rect = img.get_rect()
rect.center = (screen.get_width()/2, screen.get_height()/2)
while gameRunning == True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
pygame.quit()
break
screen.fill((255,255,255))
screen.blit(img, rect)
要使代码正常工作,您需要在“img”变量的图像位置中进行子操作,并弄乱 imgSize 变量。
比较宽高比。按最小比例缩放图像。这甚至适用于非正方形 window 分辨率。
ratio_x = screen.get_width() / img.get_width()
ratio_y = screen.get_height() / img.get_height()
scale = min(ratio_x, ratio_y)
img = pygame.transform.smoothscale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
img = pygame.image.load(r"parrot1.png")
scale = min(screen.get_width() / img.get_width(), screen.get_height() / img.get_height())
img = pygame.transform.smoothscale(img,
(round(img.get_width() * scale), round(img.get_height() * scale)))
rect = img.get_rect(center = screen.get_rect().center)
gameRunning = True
while gameRunning == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameRunning = False
screen.fill((127,127,127))
screen.blit(img, rect)
pygame.display.flip()
pygame.quit()
exit()