这是一个简单的问题,但今天出于某些原因我就是想不出来。
我需要将一张图片缩放到最大可能适合边界框的大小,同时保持纵横比。
基本上我正在寻找代码来填写这个函数:
void CalcNewDimensions(ref int w, ref int h, int MaxWidth, int MaxHeight);
其中w和h是原始高度和宽度(单位为英寸),新的高度和宽度为out,MaxWidth和MaxHeight定义了图像必须适合的边界框。
这是一个简单的问题,但今天出于某些原因我就是想不出来。
我需要将一张图片缩放到最大可能适合边界框的大小,同时保持纵横比。
基本上我正在寻找代码来填写这个函数:
void CalcNewDimensions(ref int w, ref int h, int MaxWidth, int MaxHeight);
其中w和h是原始高度和宽度(单位为英寸),新的高度和宽度为out,MaxWidth和MaxHeight定义了图像必须适合的边界框。
找出哪个更小:MaxWidth / w
或者 MaxHeight / h
,然后将w
和h
乘以这个数。
说明:
你需要找到使图像适应的缩放因子。
要找到宽度的缩放因子s
,那么s
必须满足:s * w = MaxWidth
。因此,缩放因子是MaxWidth / w
。
高度同理。
需要最大缩放的那个(较小的s
)是你必须将整个图像缩放的因子。
根据Eric的建议,我会这样做:
private static Size ExpandToBound(Size image, Size boundingBox)
{
double widthScale = 0, heightScale = 0;
if (image.Width != 0)
widthScale = (double)boundingBox.Width / (double)image.Width;
if (image.Height != 0)
heightScale = (double)boundingBox.Height / (double)image.Height;
double scale = Math.Min(widthScale, heightScale);
Size result = new Size((int)(image.Width * scale),
(int)(image.Height * scale));
return result;
}
这些强制转换可能有点过度了,但我只是试图在计算中保留精度。
return result;
- Brian Chavez如果要进行纵横比填充而不是纵横比适合,则使用较大的比率。也就是说,将Matt的代码从Math.Min更改为Math.Max。
(纵横比填充不会使边界框中留下任何空白,但可能会使一些图像超出边界,而纵横比适合则不会使图像超出边界,但可能会使一些边界框留下空白。)
ExpandToBound(new Size(640,480), new Size(66, 999)).Dump();
// {Width=66, Height=49}
ExpandToBound(new Size(640,480), new Size(999,50)).Dump();
// {Width=66, Height=50}
在另一个地方可以看到,高度分别为49和50。
这是我的代码(基于Warren先生的版本),没有任何差异并进行了轻微的重构:
// Passing null for either maxWidth or maxHeight maintains aspect ratio while
// the other non-null parameter is guaranteed to be constrained to
// its maximum value.
//
// Example: maxHeight = 50, maxWidth = null
// Constrain the height to a maximum value of 50, respecting the aspect
// ratio, to any width.
//
// Example: maxHeight = 100, maxWidth = 90
// Constrain the height to a maximum of 100 and width to a maximum of 90
// whichever comes first.
//
private static Size ScaleSize( Size from, int? maxWidth, int? maxHeight )
{
if ( !maxWidth.HasValue && !maxHeight.HasValue ) throw new ArgumentException( "At least one scale factor (toWidth or toHeight) must not be null." );
if ( from.Height == 0 || from.Width == 0 ) throw new ArgumentException( "Cannot scale size from zero." );
double? widthScale = null;
double? heightScale = null;
if ( maxWidth.HasValue )
{
widthScale = maxWidth.Value / (double)from.Width;
}
if ( maxHeight.HasValue )
{
heightScale = maxHeight.Value / (double)from.Height;
}
double scale = Math.Min( (double)(widthScale ?? heightScale),
(double)(heightScale ?? widthScale) );
return new Size( (int)Math.Floor( from.Width * scale ), (int)Math.Ceiling( from.Height * scale ) );
}
public static Size CalculateResizeToFit(Size imageSize, Size boxSize)
{
// TODO: Check for arguments (for null and <=0)
var widthScale = boxSize.Width / (double)imageSize.Width;
var heightScale = boxSize.Height / (double)imageSize.Height;
var scale = Math.Min(widthScale, heightScale);
return new Size(
(int)Math.Round((imageSize.Width * scale)),
(int)Math.Round((imageSize.Height * scale))
);
}
这是一段Python代码,也许可以指引您朝正确的方向发展:
def fit_within_box(box_width, box_height, width, height):
"""
Returns a tuple (new_width, new_height) which has the property
that it fits within box_width and box_height and has (close to)
the same aspect ratio as the original size
"""
new_width, new_height = width, height
aspect_ratio = float(width) / float(height)
if new_width > box_width:
new_width = box_width
new_height = int(new_width / aspect_ratio)
if new_height > box_height:
new_height = box_height
new_width = int(new_height * aspect_ratio)
return (new_width, new_height)
if new_width > box_width or new_height < box_height:
和:
if new_height > box_height or new_width < box_width:
- Nicolas Le Thierry d'Ennequin非常简单。 :) 问题在于找到需要将宽度和高度相乘的因子。解决方案是尝试使用一个因子,如果不适用,则使用另一个。所以...
private float ScaleFactor(Rectangle outer, Rectangle inner)
{
float factor = (float)outer.Height / (float)inner.Height;
if ((float)inner.Width * factor > outer.Width) // Switch!
factor = (float)outer.Width / (float)inner.Width;
return factor;
}
要将图片(pctRect)适应于窗口(wndRect),请按照以下方式调用
float factor=ScaleFactor(wndRect, pctRect); // Outer, inner
RectangleF resultRect=new RectangleF(0,0,pctRect.Width*factor,pctRect.Height*Factor)
根据之前的答案,这里是一个Javascript函数:
/**
* fitInBox
* Constrains a box (width x height) to fit in a containing box (maxWidth x maxHeight), preserving the aspect ratio
* @param width width of the box to be resized
* @param height height of the box to be resized
* @param maxWidth width of the containing box
* @param maxHeight height of the containing box
* @param expandable (Bool) if output size is bigger than input size, output is left unchanged (false) or expanded (true)
* @return {width, height} of the resized box
*/
function fitInBox(width, height, maxWidth, maxHeight, expandable) {
"use strict";
var aspect = width / height,
initWidth = width,
initHeight = height;
if (width > maxWidth || height < maxHeight) {
width = maxWidth;
height = Math.floor(width / aspect);
}
if (height > maxHeight || width < maxWidth) {
height = maxHeight;
width = Math.floor(height * aspect);
}
if (!!expandable === false && (width >= initWidth || height >= initHeight)) {
width = initWidth;
height = initHeight;
}
return {
width: width,
height: height
};
}
基于Jason的答案编写的Python代码,修复了放大问题,并重新排列参数以实现常规参数传递(img.shape)。
def fit_within_box(box_height, box_width, height, width):
"""
Returns a tuple (new_width, new_height) which has the property
that it fits within box_width and box_height and has (close to)
the same aspect ratio as the original size
"""
new_width, new_height = width, height
aspect_ratio = float(width) / float(height)
if new_width > box_width or new_height < box_height:
new_width = box_width
new_height = int(new_width / aspect_ratio)
if new_height > box_height or new_width < box_width:
new_height = box_height
new_width = int(new_height * aspect_ratio)
return new_height, new_width