有没有办法让我在 rmagick 中缩放和图像,我设置宽度和高度自动缩放以使图像包含相同的比例?
问问题
788 次
1 回答
4
我使用 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
希望有帮助!
于 2010-10-30T10:11:17.527 回答