【问题标题】:Resize images while preserving aspect ratio在保持宽高比的同时调整图像大小
【发布时间】:2023-01-09 00:22:00
【问题描述】:

我有一个小问题,可以有一个简单的解决方案,但不幸的是我的数学不是很好。

我有三张图片需要堆叠在一起,它们的高度加起来超过了屏幕高度。

所以为了修复,我做了一个简单的比例并改变了三个图像的高度,就像这样(这是假设的,不是实际的代码):

new_img1.height = img1.height * screen.height // (img1.height + img2.height + img3.height)

我遇到的问题是做同样的事情,但宽度,考虑到所有三个图像具有相同的宽度。

我想要的是这三个图像始终具有与原来相同的宽度,但使用新的高度调整大小(以便三个图像在两个维度上按比例变小)

我做了几次尝试,但我的数学极限对我帮助不大 XD

我该如何解决?啊,我在 Pygame 中使用 Python 3.9(尽管我认为后者不需要知道)

谢谢你们

【问题讨论】:

  • 如果您的图像宽度相同,您不妨先堆叠它们然后重新缩放......

标签: python python-3.x math pygame python-3.9


【解决方案1】:

如果要保持纵横比,则必须使用相同的缩放因子缩放图像的宽度和高度:

scale_factor1 = screen.height // (img1.height + img2.height + img3.height) 
new_img1.width = img1.width * scale_factor1 
new_img1.height = img1.height * scale_factor1 

如果你想让所有的图片都具有相同的宽度,你必须首先计算每张图片的比例因子来缩放图片的宽度。然后你可以计算高度的比例因子:

max_width = max(img1.width, img2.width, img3.width)
scale_factor1 = max_width / img1.width
scale_factor2 = max_width / img2.width
scale_factor3 = max_width / img3.width
height_scale = screen.height / (img1.height * scale_factor1 + img2.height * scale_factor2 + img3.height * scale_factor3)
scale_factor1 *= height_scale
scale_factor2 *= height_scale
scale_factor3 *= height_scale

演示该算法的最小示例:

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

update_rects = True
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.KEYDOWN:
            update_rects = True

    if update_rects:
        rect1 = pygame.Rect(0, 0, random.randrange(50, 100), random.randrange(50, 100))
        rect2 = pygame.Rect(0, 0, random.randrange(50, 100), random.randrange(50, 100))
        rect3 = pygame.Rect(0, 0, random.randrange(50, 100), random.randrange(50, 100))
        update_rects = False

    max_width = max(rect1.width, rect2.width, rect2.width)
    scale_factor1 = max_width / rect1.width
    scale_factor2 = max_width / rect2.width
    scale_factor3 = max_width / rect3.width
    height_scale = window.get_height() / (rect1.height * scale_factor1 + rect2.height * scale_factor2 + rect3.height * scale_factor3)
    scale_factor1 *= height_scale
    scale_factor2 *= height_scale
    scale_factor3 *= height_scale
    
    rect1.width *= scale_factor1
    rect1.height *= scale_factor1
    rect2.width *= scale_factor2
    rect2.height *= scale_factor2
    rect3.width *= scale_factor3
    rect3.height *= scale_factor3

    rect2.top = rect1.bottom
    rect3.top = rect2.bottom

    window.fill(0)
    pygame.draw.rect(window, "red", rect1)    
    pygame.draw.rect(window, "blue", rect2)
    pygame.draw.rect(window, "yellow", rect3)    
    pygame.display.flip()

pygame.quit()
exit()

【讨论】:

  • 我添加了一个编辑
  • 顺便说一句,这是我考虑过的事情,但正如你所看到的,它似乎不合适
  • @BlackFenix06 不要为问题添加建议的答案。这使得答案毫无用处。不适合是什么意思?你想让图像都一样吗?
  • 我想要的是调整图像的大小,使它们之间始终具有与原来相同的宽度,但随着高度的变化而缩放(这就是为什么我将图像用于视觉反馈)
  • @BlackFenix06 我明白了。我需要大约 10 分钟来添加建议。
猜你喜欢
  • 2020-10-20
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 2015-05-22
  • 2017-03-27
  • 2013-06-23
相关资源
最近更新 更多