在Ruby-On-Rails中清理OCR的映像附件

时间:2014-06-30 13:22:37

标签: ruby-on-rails ruby imagemagick ocr

如何在Ruby-On-Rails应用程序中使用Textcleaner等脚本?目前我正在使用自定义回形针处理器,我使用类似于脚本的参数。这是我的ActiveRecord中的has_attached_file行:

  has_attached_file :file, :style=> { :processors => [:text_cleaner] } }

这是Paperclip处理器:

module Paperclip
  # Handles grayscale conversion of images that are uploaded.
  class TextCleaner< Processor

    def initialize file, options = {}, attachment = nil
      super
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename, @format])
      dst.binmode

      begin
        parameters = []
        parameters << ":source"
        parameters << "-auto-orient"
        parameters << "-colorspace Gray"
        #parameters << "-sharpen 0x1"
        #parameters << "-type grayscale"
        #parameters << "-contrast-stretch 0"
        #parameters << "-clone 0"
        #parameters << "-deskew 40%"
        parameters << ":dest"

        parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

        success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
      rescue PaperclipCommandLineError => e
        raise PaperclipError, "There was an error during the grayscale conversion for #{@basename}" if @whiny
      end

      dst
    end

  end
end

1 个答案:

答案 0 :(得分:0)

使用paperclip我能够添加文本清理处理器。我通过以下方式将其添加为模型中的样式:

has_attached_file :file, :styles => { :clean => { :processors => [:text_cleaner] } }

在/lib/paperclip_processors/text_cleaner.rb我有:

module Paperclip
  # Handles grayscale conversion of images that are uploaded.
  class TextCleaner < Processor

    def initialize file, options = {}, attachment = nil
      super
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

    def make
      src = @file
      dst = Tempfile.new([@basename,@format])

      dst.binmode

      begin
        parameters = '-respect-parenthesis \( :source -colorspace gray -type grayscale -contrast-stretch 0 \) \( -clone 0 -colorspace gray -negate -lat 15x15+5% -contrast-stretch 0 \) -compose copy_opacity -composite -fill "white" -opaque none +matte -deskew 40%  -auto-orient -sharpen 0x1 :dest'
        success = Paperclip.run('convert', parameters, :source => File.expand_path(@file.path), :dest => File.expand_path(dst.path))
      rescue PaperclipCommandLineError => e
        raise PaperclipError, "There was an error during the textclean conversion for #{@basename}" if @whiny
      end

      dst
    end

  end
end