英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论