2014-06-30 47 views
0

如何在Ruby-On-Rails应用程序中使用诸如Textcleaner的脚本?目前,我正在使用自定义回形针处理器,其中使用了与脚本相似的参数。这里是我的ActiveRecord的has_attached_file线:在Ruby-On-Rails中用于OCR的干净图像附件

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

这是回形针处理器:

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

使用我回形针能添加文本清洁处理器。我添加它作为一个风格经模型:

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