Module: RubyLLM::Agents::BackgroundRemover::DSL

Includes:
Concerns::ImageOperationDSL
Defined in:
lib/ruby_llm/agents/image/background_remover/dsl.rb

Overview

DSL for configuring background removers

Provides class-level methods to configure model, output format, and edge refinement options.

Examples:

class ProductBackgroundRemover < RubyLLM::Agents::BackgroundRemover
  model "rembg"
  output_format :png
  refine_edges true
  alpha_matting true
end

Constant Summary collapse

VALID_OUTPUT_FORMATS =
%i[png webp].freeze

Instance Method Summary collapse

Methods included from Concerns::ImageOperationDSL

#cache_enabled?, #cache_for, #cache_ttl, #description, #model

Instance Method Details

#alpha_matting(value = nil) ⇒ Boolean

Set or get whether to use alpha matting

Alpha matting produces better results for hair, fur, and semi-transparent elements but is slower.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Enable alpha matting

Returns:

  • (Boolean)

    Whether alpha matting is enabled



65
66
67
68
69
70
71
72
73
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 65

def alpha_matting(value = nil)
  if value.nil?
    result = @alpha_matting
    result = inherited_or_default(:alpha_matting, false) if result.nil?
    result
  else
    @alpha_matting = value
  end
end

#background_threshold(value = nil) ⇒ Float

Set or get the background threshold

Pixels with confidence below this threshold are considered background. Higher values exclude more pixels.

Parameters:

  • value (Float, nil) (defaults to: nil)

    Threshold (0.0-1.0)

Returns:

  • (Float)

    The background threshold



100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 100

def background_threshold(value = nil)
  if value
    unless value.is_a?(Numeric) && value.between?(0.0, 1.0)
      raise ArgumentError, "Background threshold must be between 0.0 and 1.0"
    end
    @background_threshold = value.to_f
  else
    @background_threshold || inherited_or_default(:background_threshold, 0.5)
  end
end

#erode_size(value = nil) ⇒ Integer

Set or get the erode size

Size of morphological erosion applied to shrink the mask slightly to avoid edge artifacts.

Parameters:

  • value (Integer, nil) (defaults to: nil)

    Erode size in pixels

Returns:

  • (Integer)

    The erode size



118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 118

def erode_size(value = nil)
  if value
    unless value.is_a?(Integer) && value >= 0
      raise ArgumentError, "Erode size must be a non-negative integer"
    end
    @erode_size = value
  else
    @erode_size || inherited_or_default(:erode_size, 0)
  end
end

#foreground_threshold(value = nil) ⇒ Float

Set or get the foreground threshold

Pixels with confidence above this threshold are considered foreground. Lower values include more pixels.

Parameters:

  • value (Float, nil) (defaults to: nil)

    Threshold (0.0-1.0)

Returns:

  • (Float)

    The foreground threshold



82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 82

def foreground_threshold(value = nil)
  if value
    unless value.is_a?(Numeric) && value.between?(0.0, 1.0)
      raise ArgumentError, "Foreground threshold must be between 0.0 and 1.0"
    end
    @foreground_threshold = value.to_f
  else
    @foreground_threshold || inherited_or_default(:foreground_threshold, 0.5)
  end
end

#output_format(value = nil) ⇒ Symbol

Set or get the output format

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    Output format (:png, :webp)

Returns:

  • (Symbol)

    The output format



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 30

def output_format(value = nil)
  if value
    unless VALID_OUTPUT_FORMATS.include?(value)
      raise ArgumentError, "Output format must be one of: #{VALID_OUTPUT_FORMATS.join(", ")}"
    end
    @output_format = value
  else
    @output_format || inherited_or_default(:output_format, :png)
  end
end

#refine_edges(value = nil) ⇒ Boolean

Set or get whether to refine edges

When enabled, applies additional processing to smooth and refine the edges of the extracted subject.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Enable edge refinement

Returns:

  • (Boolean)

    Whether edge refinement is enabled



48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 48

def refine_edges(value = nil)
  if value.nil?
    result = @refine_edges
    result = inherited_or_default(:refine_edges, false) if result.nil?
    result
  else
    @refine_edges = value
  end
end

#return_mask(value = nil) ⇒ Boolean

Set or get whether to return the mask

When enabled, the result will include the segmentation mask in addition to the extracted foreground.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Return segmentation mask

Returns:

  • (Boolean)

    Whether to return the mask



136
137
138
139
140
141
142
143
144
# File 'lib/ruby_llm/agents/image/background_remover/dsl.rb', line 136

def return_mask(value = nil)
  if value.nil?
    result = @return_mask
    result = inherited_or_default(:return_mask, false) if result.nil?
    result
  else
    @return_mask = value
  end
end