Class: RubyLLM::Agents::BackgroundRemover

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
Execution
Defined in:
lib/ruby_llm/agents/image/background_remover.rb,
lib/ruby_llm/agents/image/background_remover/dsl.rb,
lib/ruby_llm/agents/image/background_remover/execution.rb

Overview

Background remover for subject extraction

Removes backgrounds from images using segmentation models, producing transparent PNGs or masked outputs.

Examples:

Basic usage

result = RubyLLM::Agents::BackgroundRemover.call(image: "path/to/photo.jpg")
result.url       # => "https://..." (transparent PNG)
result.has_alpha? # => true

Custom remover class

class ProductBackgroundRemover < RubyLLM::Agents::BackgroundRemover
  model "segment-anything"
  output_format :png
  refine_edges true
  alpha_matting true

  description "Removes backgrounds from product photos"
end

result = ProductBackgroundRemover.call(image: product_photo)
result.foreground # => The extracted subject
result.mask       # => Segmentation mask

Defined Under Namespace

Modules: DSL, Execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Execution

#execute

Constructor Details

#initialize(image:, **options) ⇒ BackgroundRemover

Initialize a new background remover instance

Parameters:

  • image (String, IO)

    Source image (path, URL, or IO object)

  • options (Hash)

    Additional options

Options Hash (**options):

  • :model (String)

    Model to use

  • :output_format (Symbol)

    Output format (:png, :webp)

  • :refine_edges (Boolean)

    Enable edge refinement

  • :alpha_matting (Boolean)

    Enable alpha matting for better edges

  • :return_mask (Boolean)

    Also return the segmentation mask

  • :tenant (Object)

    Tenant for multi-tenancy



75
76
77
78
79
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 75

def initialize(image:, **options)
  @image = image
  @options = options
  @tenant_id = nil
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



63
64
65
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 63

def image
  @image
end

#optionsObject (readonly)

Returns the value of attribute options.



63
64
65
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 63

def options
  @options
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



63
64
65
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 63

def tenant_id
  @tenant_id
end

Class Method Details

.call(image:, **options) ⇒ BackgroundRemovalResult

Execute background removal

Parameters:

  • image (String, IO)

    Path, URL, or IO object of the source image

  • options (Hash)

    Additional options (model, output_format, etc.)

Returns:



42
43
44
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 42

def call(image:, **options)
  new(image: image, **options).call
end

.inherited(subclass) ⇒ Object

Ensure subclasses inherit DSL settings



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 47

def inherited(subclass)
  super
  subclass.instance_variable_set(:@model, @model)
  subclass.instance_variable_set(:@output_format, @output_format)
  subclass.instance_variable_set(:@refine_edges, @refine_edges)
  subclass.instance_variable_set(:@alpha_matting, @alpha_matting)
  subclass.instance_variable_set(:@foreground_threshold, @foreground_threshold)
  subclass.instance_variable_set(:@background_threshold, @background_threshold)
  subclass.instance_variable_set(:@erode_size, @erode_size)
  subclass.instance_variable_set(:@return_mask, @return_mask)
  subclass.instance_variable_set(:@version, @version)
  subclass.instance_variable_set(:@description, @description)
  subclass.instance_variable_set(:@cache_ttl, @cache_ttl)
end

Instance Method Details

#callBackgroundRemovalResult

Execute the background removal

Returns:



84
85
86
# File 'lib/ruby_llm/agents/image/background_remover.rb', line 84

def call
  execute
end