Module: PixelFontTrieOCR::ImageUtils

Included in:
PixelFontTrieOCR
Defined in:
lib/pixel_font_trie_ocr/image_utils.rb

Instance Method Summary collapse

Instance Method Details

#bitmask(image) ⇒ Object



47
48
49
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 47

def bitmask(image)
  ImageColumnExtractor.new(image).extract
end

#mask_image(mask_columns) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 51

def mask_image(mask_columns)
  draw = new_draw
  image = new_image(mask_columns.length)
  mask_columns.each_with_index do |mask, col|
    bitmask_to_array(mask).each_with_index do |bit, row|
      draw.point(col, height - row - 1) if bit.positive?
    end
  end
  draw.draw(image)
  image
end

#new_drawObject



23
24
25
26
27
28
29
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 23

def new_draw
  draw = Magick::Draw.new
  draw.font = font_path
  draw.pointsize = font_size
  draw.fill = "black"
  draw
end

#new_image(width) ⇒ Object



17
18
19
20
21
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 17

def new_image(width)
  Magick::Image.new(width, height) do |img|
    img.background_color = "white"
  end
end

#temp_dirObject



9
10
11
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 9

def temp_dir
  @temp_dir ||= Pathname.new(__dir__).join("..", "..", "tmp")
end

#temp_dir=(value) ⇒ Object



5
6
7
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 5

def temp_dir=(value)
  @temp_dir = Pathname.new(value)
end

#temp_file(name) ⇒ Object



13
14
15
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 13

def temp_file(name)
  temp_dir.join(name).to_s
end

#text_image(text, pad: 0) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 31

def text_image(text, pad: 0)
  draw = new_draw
  metrics = draw.get_type_metrics(text)
  width = metrics.width + pad
  width = 2 if width.zero?
  puts "drawing #{text.inspect} width: #{width}" if width < 1
  image = new_image(width.ceil)
  draw.text(0, metrics.ascent.ceil, text).draw(image)
  image
end

#write_text_image(text, name, pad: 0) ⇒ Object



42
43
44
45
# File 'lib/pixel_font_trie_ocr/image_utils.rb', line 42

def write_text_image(text, name, pad: 0)
  image = text_image(text, pad: pad)
  image.write(temp_file(name))
end