Vix Hex.pm

Vix is Elixir extension for vips.

Vix is a NIF bindings for libvips. Operation binding are generated using GObject introspection, so documentation and bindings are up-to-date with the vips version installed. It uses dirty IO scheduler to avoid blocking schedulers.

Check vips operation documentation for the list of available operations and spec.

What is Vips

(from vips documentation)

libvips is a demand-driven, horizontally threaded image processing library. Compared to similar libraries, libvips runs quickly and uses little memory.

See libvips documentation for more details.

Example

alias Vix.Vips.Image
alias Vix.Vips.Operation

def example(path) do
  {:ok, im} = Image.new_from_file(path)

  # put im at position (100, 100) in a 3000 x 3000 pixel image,
  # make the other pixels in the image by mirroring im up / down /
  # left / right, see
  # https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-embed
  {:ok, im} = Operation.embed(im, 100, 100, 3000, 3000, extend: :VIPS_EXTEND_MIRROR)

  # multiply the green (middle) band by 2, leave the other two alone
  {:ok, im} = Operation.linear(im, [1, 2, 1], [0])

  # make an image from an array constant, convolve with it
  {:ok, mask} =
    Image.new_matrix_from_array(3, 3,
      [
        [-1, -1, -1],
        [-1, 16, -1],
        [-1, -1, -1]
      ],
      scale: 8
    )

  {:ok, im} = Operation.conv(im, mask, precision: :VIPS_PRECISION_INTEGER)

  # finally, write the result back to a file on disk
  :ok = Vix.Vips.Image.write_to_file(im, "out.jpg")
end

The libvips reference manual has a complete explanation of every method.

Simple unscientific comparison with mogrify

For generating thumbnail

VixMogrify
1298.731ms618.854ms
229.873ms605.824ms
334.479ms609.820ms
431.339ms604.712ms
532.526ms605.553ms

Notice that the gain is significant for the subsequent calls to the operation since vips caches the operations.

Warning

This library is experimental, untested, and unstable. Interface might change significantly in the future versions. The code is not well tested or optimized, so you might experience crashes.

Requirements

  • libvips
  • pkg-config
  • c compiler

Installation

def deps do
  [
    {:vix, "~> x.x.x"}
  ]
end

TODO

  • [ ] support VipsConnection
  • [ ] move GObject-introspection to a separate library
  • [ ] support all remaining vips types