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, preserve_input_filename: false) ⇒ ExtractedImage

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

Parameters:

  • input_source (LocalInputSource, BytesInputSource)

    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.

  • preserve_input_filename (Boolean) (defaults to: false)

    If true, keep the input source filename as-is.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mindee/image/extracted_image.rb', line 29

def initialize(input_source, page_id, element_id, preserve_input_filename: false)
  @buffer = StringIO.new(input_source.io_stream.read.to_s)
  @buffer.rewind

  @filename = if preserve_input_filename
                input_source.filename.to_s
              else
                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))
                "#{base_name}_p#{page_id}_#{element_id}#{extension}"
              end
  @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

#filenameObject (readonly)

Internal name for the file.



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

def filename
  @filename
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_input_sourceFileInputSource

Return the file as a Mindee-compatible BufferInput source.

Returns:

  • (FileInputSource)

    A BufferInput source.



84
85
86
# File 'lib/mindee/image/extracted_image.rb', line 84

def as_input_source
  as_source
end

#as_sourceFileInputSource

Return the file as a Mindee-compatible BufferInput source.

Returns:

  • (FileInputSource)

    A BufferInput source.



76
77
78
79
# File 'lib/mindee/image/extracted_image.rb', line 76

def as_source
  @buffer.rewind
  Mindee::Input::Source::BytesInputSource.new(@buffer.read || '', @filename)
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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mindee/image/extracted_image.rb', line 54

def write_to_file(output_path, file_format = nil)
  resolved_path = Pathname.new(File.expand_path(output_path))
  if file_format.nil?
    raise Error::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 Error::MindeeImageError, "Could not save file '#{output_path}'. " \
                                   'Is the provided file path valid?.'
  end
end