Class: Mindee::Image::ExtractedImage

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/image/extracted_image.rb

Overview

Generic class for image extraction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_source, page_id, element_id) ⇒ ExtractedImage

Initializes the ExtractedImage with a buffer and an internal file name.

Parameters:

  • input_source (LocalInputSource)

    Local source for input.

  • page_id (Integer)

    ID of the page the element was found on.

  • element_id (Integer, nil)

    ID of the element in a page.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mindee/image/extracted_image.rb', line 28

def initialize(input_source, page_id, element_id)
  @buffer = StringIO.new(input_source.io_stream.read.to_s)
  @buffer.rewind
  extension = if input_source.pdf?
                '.jpg'
              else
                File.extname(input_source.filename)
              end
  base_name = File.basename(input_source.filename, File.extname(input_source.filename))
  @internal_file_name = "#{base_name}_p#{page_id}_#{element_id}#{extension}"
  @page_id = page_id
  @element_id = element_id.nil? ? 0 : element_id
end

Instance Attribute Details

#bufferObject (readonly)

Buffer object of the file’s content.



18
19
20
# File 'lib/mindee/image/extracted_image.rb', line 18

def buffer
  @buffer
end

#element_idObject (readonly)

ID of the element on a given page.



15
16
17
# File 'lib/mindee/image/extracted_image.rb', line 15

def element_id
  @element_id
end

#internal_file_nameObject (readonly)

Internal name for the file.



21
22
23
# File 'lib/mindee/image/extracted_image.rb', line 21

def internal_file_name
  @internal_file_name
end

#page_idObject (readonly)

ID of the page the image was extracted from.



12
13
14
# File 'lib/mindee/image/extracted_image.rb', line 12

def page_id
  @page_id
end

Instance Method Details

#as_sourceFileInputSource

Return the file as a Mindee-compatible BufferInput source.

Returns:

  • (FileInputSource)

    A BufferInput source.



70
71
72
73
# File 'lib/mindee/image/extracted_image.rb', line 70

def as_source
  @buffer.rewind
  Mindee::Input::Source::BytesInputSource.new(@buffer.read, @internal_file_name)
end

#write_to_file(output_path, file_format = nil) ⇒ Object

Saves the document to a file.

extension if not provided.

Parameters:

  • output_path (String)

    Path to save the file to.

  • file_format (String, nil) (defaults to: nil)

    Optional MiniMagick-compatible format for the file. Inferred from file

Raises:

  • (MindeeError)

    If an invalid path or filename is provided.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mindee/image/extracted_image.rb', line 48

def write_to_file(output_path, file_format = nil)
  resolved_path = Pathname.new(File.expand_path(output_path))
  if file_format.nil?
    raise Errors::MindeeImageError, 'Invalid file format.' if resolved_path.extname.delete('.').empty?

    file_format = resolved_path.extname.delete('.').upcase
  end
  begin
    @buffer.rewind
    image = MiniMagick::Image.read(@buffer)
    image.format file_format.to_s.downcase
    image.write resolved_path.to_s
    logger.info("File saved successfully to '#{resolved_path}'")
  rescue StandardError
    raise Errors::MindeeImageError, "Could not save file '#{output_path}'. " \
                                    'Is the provided file path valid?.'
  end
end