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



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

def self.detect_image_processor(detector: DEFAULT_BINARY_DETECTOR)
  case
  when detector.call("magick"), detector.call("convert"), detector.call("gm")
    :minimagick
  when detector.call("vips")
    :vips
  end
end

Instance Method Details

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



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

def compose(color_file:, alpha_file:, destination_path:)
  image = case configured_image_processor
    when :vips
      then vips_compose(color_file: color_file, alpha_file: alpha_file)
    when :minimagick
      then minimagick_compose(color_file: color_file, alpha_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