Rails Ruby-vips 下通过 composite 进行 watermark 测试

lukefan · 2019年03月31日 · 最后由 mimosa 回复于 2019年04月15日 · 1542 次阅读

最开始处理图像都是使用 rmagick,后来说是 rmagick 内存泄漏,就开始使用 mini_magick。 最近,看到 active_storage 里面提到了一种新的图像处理库,vips。

于是打开看了看,发现 vips 的效率奇高,基本上比 mini_magick 快上很多。 所有 vips 的 github 库,都写着 benchmark,各种速度比较。 于是就拿过来玩耍了一下,测试了一下水印。

composite 函数,就是合成。 基本流程应该是这样的:

require 'vips'
img = Vips::Image.new_from_file 'DSC00536.JPG'
img = img.resize 500.0 / img.width
logo = Vips::Image.new_from_file 'film.png'
logo = logo.gravity 'south-east', img.width, img.height
img.composite(logo, :over).write_to_file 'watermarked.jpg'

这里面,需要注意的是 composite 中的参数,blend_mode 具体的解释如下: https://github.com/libvips/ruby-vips/blob/master/lib/vips/blend_mode.rb

下面是相关的各种实验数据附件: 原始图片: 水印的 logo: 以及各种 blend_mode 的结果: :clear - where the second object is drawn, the first is removed :source - the second object is drawn as if nothing were below :over - the image shows what you would expect if you held two semi-transparent slides on top of each other :in - the first object is removed completely, the second is only drawn where the first was :out - the second is drawn only where the first isn't :atop - this leaves the first object mostly intact, but mixes both objects in the overlapping area :dest - leaves the first object untouched, the second is discarded completely :dest_over - like :over, but swaps the arguments :dest_in - like :in, but swaps the arguments :dest_out - like :out, but swaps the arguments :dest_atop - like :atop, but swaps the arguments :xor - something like a difference operator :add - a bit like adding the two images :saturate - a bit like the darker of the two :multiply - at least as dark as the darker of the two inputs :screen - at least as light as the lighter of the inputs :overlay - multiplies or screens colors, depending on the lightness :darken - the darker of each component :lighten - the lighter of each component :colour_burn - darken first by a factor of second :colour_dodge - brighten first by a factor second :hard_light - multiply or screen, depending on lightness :soft_light - darken or lighten, depending on lightness :difference - difference of the two :exclusion - somewhat like :difference, but lower-contrast


gazeldx 回复


需要 登录 后方可回复, 如果你还没有账号请 注册新账号