Class: UdiseCaptchaReader::ImagePreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/udise_captcha_reader/image_preprocessor.rb

Class Method Summary collapse

Class Method Details

.preprocess_image(image_path, approach) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/udise_captcha_reader/image_preprocessor.rb', line 6

def self.preprocess_image(image_path, approach)
  # Get original filename for trimming
  original_filename = File.basename(image_path, ".*")
  
  # Open and process the image
  image = MiniMagick::Image.open(image_path)
  
  # First trim the whitespace and get the trimmed image
  trimmed_path = File.join(Dir.pwd, 'tmp', "#{original_filename}_trimmed.png")
  FileUtils.mkdir_p(File.dirname(trimmed_path))
  
  # Clone and trim the image
  trimmed = MiniMagick::Image.create(".png") do |f|
    image.format "png"
    image.write f.path
  end
  
  # Trim white space from left and right
  trimmed.combine_options do |b|
    b.fuzz "10%" # Allow some tolerance for off-white pixels
    b.trim "+repage"
  end
  
  # Save trimmed image
  trimmed.write(trimmed_path)
  
  # Open trimmed image for further processing
  image = MiniMagick::Image.open(trimmed_path)
  temp_output = Tempfile.new(['processed', '.png'])
  
  case approach
  when :split_characters
    # Split characters preprocessing - optimized for white bg, black text
    image.combine_options do |b|
      b.resize "300%"
      b.threshold "50%"  # Simple threshold for already well-contrasted images
      b.border "5x5"
      b.bordercolor "white"
    end
  when :standard
    # Standard preprocessing - optimized for white bg, black text
    image.combine_options do |b|
      b.resize "250%"
      b.threshold "50%"  # Simple threshold for black text
      b.border "5x5"
      b.bordercolor "white"
    end
  when :high_contrast
    # High contrast preprocessing - optimized for white bg, black text
    image.combine_options do |b|
      b.resize "300%"
      b.threshold "50%"  # Simple threshold for black text
      b.border "10x10"
      b.bordercolor "white"
    end
  when :character_separation
    # Character separation focused preprocessing
    image.combine_options do |b|
      b.resize "400%"
      b.threshold "50%"  # Simple threshold for black text
      b.border "15x15"
      b.bordercolor "white"
    end
  end

  image.write(temp_output.path)
  temp_output
end

.trim_whitespace(image) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/udise_captcha_reader/image_preprocessor.rb', line 75

def self.trim_whitespace(image)
  # Create tmp directory if it doesn't exist
  tmp_dir = File.join(Dir.pwd, 'tmp')
  FileUtils.mkdir_p(tmp_dir)
  
  # Get original filename from the image path
  original_filename = File.basename(image.path, ".*")
  
  # Create path for trimmed image in tmp folder
  trimmed_path = File.join(tmp_dir, "#{original_filename}_trimmed.png")
  
  # Clone the image to avoid modifying the original
  trimmed = MiniMagick::Image.create(".png") do |f|
    image.format "png"
    image.write f.path
  end
  
  # Trim white space from left and right
  trimmed.combine_options do |b|
    b.fuzz "10%" # Allow some tolerance for off-white pixels
    b.trim "+repage"
  end
  
  # Save to tmp folder
  trimmed.write(trimmed_path)
  
  # Return as a File object
  File.new(trimmed_path)
end