Class: OcrFile::ImageEngines::ImageMagick

Inherits:
Object
  • Object
show all
Defined in:
lib/ocr-file/image_engines/image_magick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_path:, temp_path:, save_file_path:, config:) ⇒ ImageMagick

Returns a new instance of ImageMagick.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ocr-file/image_engines/image_magick.rb', line 10

def initialize(image_path:, temp_path:, save_file_path:, config:)
  @image_path = image_path
  @config = config
  @save_file_path = save_file_path

  @temp_path = temp_path

  # Will be available in the next version of MiniMagick > 4.11.0
  # https://github.com/minimagick/minimagick/pull/541
  # MiniMagick.configure do |config|
  #   # cli_version  graphicsmagick?  imagemagick7?  imagemagick? version
  #   config.tmpdir = File.join(Dir.tmpdir, @temp_path)
  # end

  @image = MiniMagick::Image.open(image_path)

  @width = @image[:width]
  @height = @image[:height]
end

Instance Attribute Details

#configObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def config
  @config
end

#heightObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def height
  @height
end

#imageObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def image
  @image
end

#image_pathObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def image_path
  @image_path
end

#save_file_pathObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def save_file_path
  @save_file_path
end

#temp_pathObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def temp_path
  @temp_path
end

#widthObject (readonly)

TODO: Conversion of image types Rotation and detection of skew



8
9
10
# File 'lib/ocr-file/image_engines/image_magick.rb', line 8

def width
  @width
end

Instance Method Details

#bwObject



55
56
57
58
# File 'lib/ocr-file/image_engines/image_magick.rb', line 55

def bw
  @image.alpha('off')
  @image.auto_threshold("otsu")
end

#convert!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ocr-file/image_engines/image_magick.rb', line 30

def convert!
  return @image_path unless @config[:image_preprocess]

  if @config[:dimensions].is_a?(Array) && @config[:dimensions].size == 2
    resize(width, height)
  end

  @config[:effects].each do |effect|
    self.send(effect.to_sym)
  end

  save!
end

#deskewObject



82
83
84
# File 'lib/ocr-file/image_engines/image_magick.rb', line 82

def deskew
  @image.deskew('40%') # threshold recommended in the docs
end

#despeckleObject



86
87
88
# File 'lib/ocr-file/image_engines/image_magick.rb', line 86

def despeckle
  @image.despeckle
end

#enhanceObject



60
61
62
# File 'lib/ocr-file/image_engines/image_magick.rb', line 60

def enhance
  @image.enhance
end

#normObject



64
65
66
# File 'lib/ocr-file/image_engines/image_magick.rb', line 64

def norm
  @image.equalize
end

#remove_shadowObject



76
77
78
79
80
# File 'lib/ocr-file/image_engines/image_magick.rb', line 76

def remove_shadow
  @image.negate
  @image.lat("20x20+10\%")
  @image.negate
end

#resize(width, height) ⇒ Object



49
50
51
# File 'lib/ocr-file/image_engines/image_magick.rb', line 49

def resize(width, height)
  @image.resize("#{width}x#{height}")
end

#save!Object



44
45
46
47
# File 'lib/ocr-file/image_engines/image_magick.rb', line 44

def save!
  image.write(@save_file_path)
  @save_file_path
end

#sharpenObject

Most likely not going to be configurable because these are aggressive parameters used to optimised OCR results and not the final results of the PDFs



71
72
73
# File 'lib/ocr-file/image_engines/image_magick.rb', line 71

def sharpen
  @image.sharpen('0x4') # radiusXsigma
end