ruby-vips Documentation

repository·master·Indexed 21 days ago

https://github.com/libvips/ruby-vips

A Ruby binding for the libvips image processing library using ruby-ffi. It provides high-performance, low-memory image manipulation capabilities suitable for demand-driven and horizontally threaded processing. The library automatically wraps libvips operations, allowing for lazy loading, efficient in-place modifications via MutableImage, and support for various image sources including files, memory buffers, and Ruby arrays.

Tokens
8.1K
Snippets
42
Records
52
Agent score
74%

What's inside ruby-vips

  1. Install ruby-vips on Linux and macOS

    master

    To install ruby-vips, you must first install the libvips binary using your system's package manager. For example, use apt install libvips42 on Debian/Ubuntu or brew install vips on macOS. Once the binary is installed, install the gem via the command line or add it to your Gemfile.

    Steps:

    1. Install libvips binary via package manager.
    2. Run gem install ruby-vips or add gem "ruby-vips" to your Gemfile.
    gem install ruby-vips
  2. Install ruby-vips on Windows

    master

    On Windows, the ruby-vips gemspec automatically pulls in the msys libvips dependency. You only need to install the gem itself. It has been tested with Ruby and MSYS from Chocolatey (choco).

    gem install ruby-vips
  3. Write images to a Vips::Target

    master

    A Vips::Target represents a destination for image data. You can create different types of targets (files, memory, or file descriptors) and then use the Image#write_to_target method to write an image to that destination. When writing to a memory target, you can retrieve the resulting data using Object#get("blob").

    target = Vips::Target.new_to_file('k2.jpg')
    image.write_to_target(target, '.jpg')
  4. How Vips operations are invoked via method_missing

    master

    The Vips::Image class uses method_missing to allow calling any libvips operation as if it were a method on the image object. If a method name corresponds to a valid VipsOperation nickname, it will be executed with the image as the first argument.

    # If 'invert' is a valid vips operation:
    new_image = image.invert

    This also works on the class level: Vips::Image.method_name(args).

    # Instead of explicit operation calls, use nicknames directly
    new_image = image.some_vips_operation(arg1, arg2)
  5. Mutate an image in-place

    master

    While Vips::Image objects are generally immutable, you can perform multiple modifications efficiently using the mutate method. This provides a Vips::MutableImage object within a block, allowing you to set or remove metadata and modify pixels without creating intermediate image objects.

    image = image.mutate do |x|
      (0 ... 1).step(0.01) do |i|
        x.draw_line! 255, x.width * i, 0, 0, x.height * (1 - i)
      end
    end
    image = image.mutate do |x|
      x.set! "some-metadata", "value"
    end
  6. How to use Vips::Region to fetch pixel data

    master

    A Vips::Region represents a specific area of an image. You can create a region from an existing image object and then use the fetch method to quickly retrieve a block of raw pixel data from that region. This is useful for high-performance access to specific rectangular areas of an image.

    To use it:

    1. Initialize a new region with an image: region = Vips::Region.new(image).
    2. Call fetch(left, top, width, height) to get the bytes.

    Note: fetch requires libvips version 8.8 or higher.

    region = Vips::Region.new(image)
    pixels = region.fetch(10, 10, 100, 100)
  7. How ruby-vips maps libvips operations to Ruby methods

    master

    The ruby-vips binding uses Image#method_missing to automatically wrap libvips operations. This means the Ruby API always matches the current libvips shared library version.

    Mapping Rules:

    • Instance Methods: A libvips operation like vips_add() appears as Image#add. The first input image is automatically set to self.
    • Class Methods: Operations that do not take an input image (e.g., Vips::Image.black) appear as class methods.
    • Arguments: Remaining arguments are passed to the operation. Trailing keyword arguments are used to set operation options.
    • Return Values: If an operation has one result, it returns that result. If it has multiple, it returns an array. If it has optional output objects, they are returned as a final hash.
    • Chaining: Since operations return the result image, you can chain them: image.real.cos.
    • Type Conversion: The wrapper automatically converts Ruby types (integers, floats, arrays) to the required libvips types (e.g., VipsArrayDouble).
    # Example of automatic wrapping and chaining
    result_image = image.real.cos
    
    # Example of handling optional outputs via a hash
    min_value, opts = image.min x: true, y: true
    x_pos = opts['x']
    y_pos = opts['y']
    
    # Example of using constants for multiple inputs
    result_image = condition_image.ifthenelse [0, 255, 0], [255, 0, 0]
  8. How to perform in-place image modifications with `Vips::Image#mutate`

    master

    In ruby-vips, standard Vips::Image objects are immutable. To perform in-place modifications (mutations) on an image, you should use the Vips::Image#mutate method. This method provides a Vips::MutableImage object within a block.

    Inside the block, you can call VIPS operations that modify the image in-place. Once the block finishes, the method returns the modified Vips::Image.

    Important: Vips::MutableImage is intended for internal use via the mutate interface. Do not attempt to instantiate Vips::MutableImage directly.

    # Example pattern (conceptual usage)
    # image.mutate do |x|
    #   x.draw_point!(ink, left, top)
    #   x.set!("metadata_key", "value")
    # end
  9. Read and write image metadata

    master

    Images in libvips can hold metadata fields (e.g., icc-profile-data). Because images are immutable, you must use a mutate block to modify metadata.

    Reading Metadata:

    • Image#get_fields: Returns an array of supported metadata field names.
    • Image#get_typeof(field): Returns the type of a specific field.
    • Image#get(field): Returns the value of the field converted to a Ruby type.

    Writing Metadata:

    • Use MutableImage#set!(field, value) to change a value.
    • Use MutableImage#set_type!(field, type) to create a new field with a specific type.
    # Remove all metadata except the ICC profile
    image = image.mutate do |mutable|
      image.get_fields.each do |field|
        mutable.remove! field unless field == "icc-profile-data"
      end
    end
  10. Use MutableImage for efficient draw operations

    master

    Standard libvips operations are immutable and return a new image. If you perform many draw operations (like draw_line or draw_circle) on a standard Image, ruby-vips will create a full copy of the image for every single call, which is extremely slow and memory-intensive.

    To modify an image in-place, use Image#mutate to obtain a MutableImage. Within the block, use the ! version of draw operations (e.g., draw_line!) to modify the image directly without copying.

    image = image.mutate do |mutable|
      (0 ... 1).step(0.01) do |i|
        # Use the bang (!) version to modify in-place
        mutable.draw_line! 255, mutable.width * i, 0, 0, mutable.height * (1 - i)
      end
    end