Sha256: 1aaa83bc55157db80e4792cd6f0c43233140f4f6db431bbb1f5802caf646f3e3

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

require 'RMagick'

# An Extractor takes Boundries object and a layout pattern and returns a Sqed::Result
# 
class Sqed::Extractor

  attr_accessor :boundaries, :layout, :image

  def initialize(boundaries: boundaries, layout: layout, image: image)
    raise if boundaries.nil? || !boundaries.class == Sqed::Boundaries
    raise if layout.nil? || !layout.class == Hash

    @layout = layout
    @boundaries = boundaries
    @image = image
  end

  def result
    r = Sqed::Result.new()
   
    # assign the images to the result
    boundaries.each do |section, coords|
      r.send("#{LAYOUT_SECTION_TYPES[section]}=", extract_image(coords))
    end 

    # assign the metadata to the result
    layout.keys.each do |section_index, section_type|
      # only extract data if a parser exists
      if parser = SECTION_PARSERS[section_type]
        r.send("#{section_type}=", parser.new(image: r.send(section_type + "_image").text) )
      end
    end

    r
  end

  # coords are x1, y1, x2, y2
  def extract_image(coords)
    # crop takes x, y, width, height
    # @image.crop(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1] )
    bp = 0
    @image.crop(coords[0], coords[1], coords[2], coords[3], true)
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sqed-0.0.4 lib/sqed/extractor.rb
sqed-0.0.3 lib/sqed/extractor.rb
sqed-0.0.2 lib/sqed/extractor.rb
sqed-0.0.1 lib/sqed/extractor.rb