Class: RemoveBg::ImageComposer

Inherits:
Object
  • Object
show all
Defined in:
lib/remove_bg/image_composer.rb

Overview

Combines alpha.png and color.jpg files to produce a full-sized transparent PNG. An image processing library (ImageMagick, GraphicsMagick, or libvips) must be available on the system.

See Also:

Constant Summary collapse

DEFAULT_BINARY_DETECTOR =
lambda do |binary_name|
  system("which", binary_name, out: File::NULL)
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_image_processor(detector: DEFAULT_BINARY_DETECTOR) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/remove_bg/image_composer.rb', line 16

def self.detect_image_processor(detector: DEFAULT_BINARY_DETECTOR)
  if detector.call("magick") || detector.call("convert") || detector.call("gm")
    :minimagick
  elsif detector.call("vips")
    :vips
  end
end

Instance Method Details

#compose(color_file:, alpha_file:, destination_path:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/remove_bg/image_composer.rb', line 24

def compose(color_file:, alpha_file:, destination_path:)
  image = case configured_image_processor
          when :vips
            vips_compose(color_file:, alpha_file:)
          when :minimagick
            minimagick_compose(color_file:, alpha_file:)
          when nil
            raise RemoveBg::Error, "Please configure an image processor to use image composition"
          else
            raise RemoveBg::Error, "Unsupported image processor: #{configured_image_processor.inspect}"
          end

  image.call(destination: destination_path)
end