我试图找到答案,但我不能。
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)