【问题标题】:Maths to resize an image into bounds of max height and max width将图像大小调整为最大高度和最大宽度范围的数学
【发布时间】:2010-09-09 11:34:21
【问题描述】:

给定一张图片:

maxWidth = 400;
maxHeight = 200;
width = photo.Width;
height = photo.Height;

如果任一尺寸超过最大属性,我将如何缩放图像?

这里有一些测试用例:

300x300  :   Too tall, but width ok.
500x200  :   Too wide, but height ok.
650x300  :   Too tall and too wide
300x190  :   Fine, don't resize

我在可视化数学时遇到了麻烦,如果它太简单了,抱歉!给我最麻烦的情况是两个尺寸都超过了允许的最大值。

【问题讨论】:

    标签: image-processing resize scaling


    【解决方案1】:

    分别计算所需的垂直和水平缩放,然后选择两者中较小的一个并将结果限制为最大值 1。在代码中:

    scale = min(1, min(maxWidth/photo.Width, maxHeight/photo.Height))
    

    确保除法运算使用浮点运算。如何做到这一点因语言而异。在 C/Java/C# 及其同类中,将操作数之一转换为浮点数。

    【讨论】:

      【解决方案2】:

      计算两个比率(带有浮点结果):

      • 输入宽度除以最大允许宽度
      • 输入高度除以最大允许高度

      那么,

      • 如果两个比率均
      • 如果其中一个比率 > 1.0,则按该系数缩小。
      • 如果两个比率都 > 1.0,则按两个因子中较大的一个按比例缩小。

      【讨论】:

        【解决方案3】:

        我的数学很差,但是,假设你想要一个比例尺,我会这样解决它:

        if maxWidth < photo.Width
            width = 'too big'
        if maxHeight < photo.Height
            height = 'too big'
        
        if height == 'to big' & width == 'too big' 
            x = photo.Width / maxWidth;
            y = photo.Height / maxHeight;
            if x > y
                scale_by_width(photo)
            else
                scale_by_height(photo)
        
        elseif height == 'too big'
            scale_by_height(photo)
        
        elseif width == 'too big'
            scale_by_width(photo)
        
        else
            do_nothing
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-28
          • 2017-12-08
          • 1970-01-01
          • 2016-03-30
          • 2014-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多