【问题标题】:rmagick auto scaling with proportionsrmagick 自动缩放比例
【发布时间】:2010-10-30 18:26:00
【问题描述】:

有没有办法让我在 rmagick 中设置宽度和高度自动缩放以使图像包含相同的比例?

【问题讨论】:

    标签: ruby rmagick


    【解决方案1】:

    我使用 resize_to_fit 方法,它将使用提供的参数作为最大宽度/高度,但会保持纵横比。所以,是这样的:

    @scaled = @image.resize_to_fit 640 640
    

    这将确保宽度或高度不大于 640,但不会拉伸图像使其看起来很有趣。因此,您最终可能会得到 640x480 或 480x640。还有一个resize_to_fit!就地转换的方法

    如果你想在不考虑边界框的情况下调整到给定的宽度,你将不得不编写一个辅助函数。像这样的:

    @img = Magick::Image::read(file_name).first
    
    def resize_by_width image new_width  
      @new_height = new_width * image.x_resolution.to_f / image.y_resolution.to_f
      new_image = image.scale(new_width, new_height)
    
      return new_image
    end
    
    @resized = resize_by_width @img 1024
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 2015-06-16
      • 2015-06-10
      • 2019-03-20
      • 2012-07-28
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多