安装:
sudo apt-get install imagemagick
ImageMagick 是一个用来创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线, 多边形,椭圆,曲线,附加到图片伸展旋转。ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布。支持大多数的操作系统。
检测是否支持指定格式 identify -list format | grep PNG
sudo apt-get install tesseract-ocr
OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程。
Tesseract:开源的OCR识别引擎,初期Tesseract引擎由HP实验室研发,后来贡献给了开源软件业,后经由Google进行改进,消除bug,优化,重新发布
gem install mini_magick
mini_magick 是用ruby对ImageMagick命令的封装,可以这样简单的使用
image = MiniMagick::Image.open("input.jpg") image.resize "100x100" image.write "output.jpg"
image = MiniMagick::Image.open("http://www.google.com/images/logos/logo.png") image.resize "5x5" image.format "gif" image.write "localcopy.gif"
https://github.com/minimagick/minimagick
gem install rtesseract
rtesseract对了tesseract-ocr进行了封装
识别验证码:
RTesseract.new(img.path).to_s
在 ubuntu12.04上安装如果出现如下问题,请安装 libmagickwand-dev
sudo apt-get install libmagickwand-dev
zhaol-a@ubuntu:~/gcj_project$ gem install rtesseract
/usr/share/ruby-rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Building native extensions. This could take a while...
ERROR: Error installing rtesseract:
ERROR: Failed to build gem native extension.
/usr/share/ruby-rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... no
Can't install RMagick 2.13.2. Can't find Magick-config in /usr/share/ruby-rvm/gems/ruby-1.9.3-p392/bin:/usr/share/ruby-rvm/gems/ruby-1.9.3-p392@global/bin:/usr/share/ruby-rvm/rubies/ruby-1.9.3-p392/bin:/usr/share/ruby-rvm/gems/ruby-1.9.3-p392/bin:/usr/share/ruby-rvm/gems/ruby-1.9.3-p392@global/bin:/usr/share/ruby-rvm/rubies/ruby-1.9.3-p392/bin:/usr/share/ruby-rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/share/ruby-rvm/rubies/ruby-1.9.3-p392/bin/ruby
使用mechanize,nokogiri进行数据抓取,验证码识别的例子:
#encoding: utf-8 require 'mechanize' require 'nokogiri' require 'spreadsheet' require 'rtesseract' require 'mini_magick' require 'open-uri' require 'uri' require 'iconv' require 'active_support/all' #调整图片的大小来提高图片的识别准确度 def parse_img_to_str(img_url) img = MiniMagick::Image.open(img_url) img.resize '200%x200%' # 放大 img.colorspace("GRAY") # 灰度化 img.monochrome # 去色 str = RTesseract.new(img.path).to_s # 识别 File.unlink(img.path) # 删除临时文件 if str.nil? return nil else return str.strip.to_f end end #使用异常处理,可以减少因为网络延迟导致timeout的问题,但是不能完全避免 #分析请求的url, 和参数 def get_json_data(agent, url, params) begin response = agent.post(url, params) json_data = JSON.parse(response.body) return json_data rescue retry # restart from beginning end end p "waiting................." agent = Mechanize.new agent.open_timeout = 15 file_path = File.dirname(__FILE__)user #用户登陆要抓取的网站后,用firefox导出cookies信息到cookies.txt cookies_file_path = "/home/username/project/cookies.txt" #导入cookie信息,跳过登陆 agent.cookie_jar.load_cookiestxt(cookies_file_path) #抓取年份信息的例子 title_params = {"area" => area,"province" => "北京"} years_info = get_json_data(agent, gov_mat_ttile_url, title_params)["result"]
欢迎关注微信订阅号:ruby程序员