我们经常要处理图片,比如搞个什么头像缩略图,在所难免要对图片进行裁剪、缩放等操作,下面就来总结一下Ruby中图片裁剪、缩放的类库。
ImageScience:
实现图片放缩的库,使用前需安装freeimage和rubyinline。
网址:http://seattlerb.rubyforge.org/ImageScience.html
RMagick:
RMagick是Ruby语言跟ImageMagick图形处理程序之间的接口,Ruby程序可以利用RMagick对图像进行缩略、剪裁等等的一系列操作。
网址:http://rmagick.rubyforge.org
等比缩放:
800x800 => 60x90 实际结果: 60x60
800x800 => 135x90 实际结果: 90x90
800x800 => 135x900 实际结果: 135x135
400x900 => 60x60 实际结果: 60x26
400x900 => 135x900 实际结果: 308x135
MiniMagick:
它是RMagick的精简,直接用Ruby包装ImageMagick command line。避免了RMagick的内存问题。
http://rubyforge.org/projects/mini-magick
1.install imagescience
a)install FreeImage
download freeimage from :http://sourceforge.net/projects/freeimage/files/
$unzip FreeImage3130.zip
$make
$sudo make install
b)install RubyInline
$sudo gem install RubyInline
c)install ImageScience
http://seattlerb.rubyforge.org/ImageScience.html
$sudo apt-get install libfreeimage3 libfreeimage-dev
$sudo gem install image_science
centos 下是安装ImageMagick
download from http://www.imagemagick.org/script/download.php
make & make install
2.install rmagick
a) install imagemagick
$sudo apt-get install imagemagick
$sudo apt-get install libmagickwand-dev
b) install ruby rmagick
download rmagick from http://rmagick.rubyforge.org/
$tar xzvf RMagick-2.12.....
$ruby setup.rb
$ruby setup.rb install
--------------------------------------------------------------------------------
$ sudo apt-get install imagemagick
$ dpkg -l |grep imagemagick
imagemagick
imagemagick-doc
$ convert
$ whereis convert
$ which is convert
$ convert -compress none -depth 8 -alpha off zhejiang.gif zhejiang.tif
enlarge the image can improve ocr accuracy
I believe the real challenge to apply ocr for plate recognition is
that the plate image are "too dirty" comparing to paper documents.
There are frames, skews, un-even shadows, etc. You have to do your own
work to parse the plate into separate chars and feed the ocr engine. I
don't think tesseract itself can handle this automatically given the
raw image. But I believe it will do pretty well once you get the
binarized separate chars. Basically, plate recognition is more a image
processing problem than ocr problem.
You can use the grammar as post-process to make corrections.
to convert the pdf I used Image Magick convert application. bellow the set command that I use.
convert -density 288 src.pdf -colorspace Gray -depth 8 -alpha off tmp.tif
tesseract tmp.tif out.txt
how to eliminate noise
def upload_file(file)
if !file.original_filename.empty?
@filename = file.original_filename
File.open("#{RAILS_ROOT}/public/images/#{@filename}", "wb") do |f|
f.write(file.read)
end
return @filename
end
end
# params[:file] is Tempfile obj from the page
img = Magick::Image.from_blob(params[:file].read)[0]
img.write("#{filename}")