如何将PIL.Image调整为最接近3的倍数。

huangapple go评论77阅读模式
英文:

How to resize PIL.Image to the closest multiples of 3

问题

我有一张图像(100,100)。我想将其调整为最接近3的倍数。在这种情况下,我想将其调整为(99,99)。

有没有一种好的方法来编写这个算法?

英文:

Say I have an image (100, 100). I want to resize it to the closest multiple of 3. In this case I want to resize it to (99, 99).

Is there a good way to code this algorithm?

答案1

得分: 1

你应该先将输入的数字除以3,然后四舍五入,然后再乘以3。这个函数应该适用于你。

def closest_multiple(original: int, multiple_of: int) -> int:
    """
    返回距离 `original` 最接近的 `multiple_of` 的倍数。
    
    >>> closest_multiple(9, 3)
    9
    >>> closest_multiple(10, 3)
    9
    >>> closest_multiple(11, 3)
    12
    """
    
    return int(round(original / multiple_of)) * multiple_of
英文:

You should first divide the input number by 3, then round it, and then multiply it by 3 again. This function should do the trick for you.

def closest_multiple(original: int, multiple_of: int) -> int:
    """
    Return the closest multiple of `multiple_of` to `original`.
    
    >>> closest_multiple(9, 3)
    9
    >>> closest_multiple(10, 3)
    9
    >>> closest_multiple(11, 3)
    12
    """
    
    return int(round(original / multiple_of)) * multiple_of

huangapple
  • 本文由 发表于 2023年2月7日 02:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定