本文介绍了通过像素数量调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到答案,但我不能。
I tried to find out, but I couldn't.
有一个形象,例如, 241x76
共拥有的18316像素(241 * 76)
。调整大小规则,像素数无法通过 10,000
。然后,我怎么能得到新的大小保持纵横比和获得小于 10,000
像素?
A image, for example, 241x76
has a total of 18,316 pixels (241 * 76)
.The resize rule is, the amount of pixels cannot pass 10,000
.Then, how can I get the new size keeping the aspect ratio and getting less than 10,000
pixels?
推荐答案
伪code:
pixels = width * height
if (pixels > 10000) then
ratio = width / height
scale = sqrt(pixels / 10000)
height2 = floor(height / scale)
width2 = floor(ratio * height / scale)
ASSERT width2 * height2 <= 10000
end if
记住使用浮点运算涉及比
和比例
实施的时候。
的Python
import math
def capDimensions(width, height, maxPixels=10000):
pixels = width * height
if (pixels <= maxPixels):
return (width, height)
ratio = float(width) / height
scale = math.sqrt(float(pixels) / maxPixels)
height2 = int(float(height) / scale)
width2 = int(ratio * height / scale)
return (width2, height2)
这篇关于通过像素数量调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!