Class: OcrFile::ImageEngines::ImageMagick
- Inherits:
-
Object
- Object
- OcrFile::ImageEngines::ImageMagick
- Defined in:
- lib/ocr-file/image_engines/image_magick.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#height ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#image ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#image_path ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#save_file_path ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#temp_path ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
-
#width ⇒ Object
readonly
TODO: Conversion of image types Rotation and detection of skew.
Instance Method Summary collapse
- #bw ⇒ Object
- #convert! ⇒ Object
- #deskew ⇒ Object
- #despeckle ⇒ Object
- #enhance ⇒ Object
-
#initialize(image_path:, temp_path:, save_file_path:, config:) ⇒ ImageMagick
constructor
A new instance of ImageMagick.
- #norm ⇒ Object
- #remove_shadow ⇒ Object
- #resize(width, height) ⇒ Object
- #save! ⇒ Object
-
#sharpen ⇒ Object
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.
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
#config ⇒ Object (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 |
#height ⇒ Object (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 |
#image ⇒ Object (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_path ⇒ Object (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_path ⇒ Object (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_path ⇒ Object (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 |
#width ⇒ Object (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
#bw ⇒ Object
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 |
#deskew ⇒ Object
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 |
#despeckle ⇒ Object
86 87 88 |
# File 'lib/ocr-file/image_engines/image_magick.rb', line 86 def despeckle @image.despeckle end |
#enhance ⇒ Object
60 61 62 |
# File 'lib/ocr-file/image_engines/image_magick.rb', line 60 def enhance @image.enhance end |
#norm ⇒ Object
64 65 66 |
# File 'lib/ocr-file/image_engines/image_magick.rb', line 64 def norm @image.equalize end |
#remove_shadow ⇒ Object
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 |
#sharpen ⇒ Object
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 |