「ruby」使用rmagick處理圖像

安裝rmagick gemhtml

A new release 2.13.2 of RMagick is now available on github as well as rubygems.
This release will fix the installation issues due to ruby 1.9.3 and ImageMagick 6.8+. You can install it the usual way with bundle by adding to Gemfile:
gem 'rmagick', '2.13.2'
or if you want to be on edge:
gem 'rmagick', '2.13.2', :git=>'http://github.com/rmagick/rmagick.git'
You also install it directly by doing:
gem install rmagick

 

RMagick的基本使用

(1)定義主要對象


使用RMagick,重要會用到兩個對象:Image和Draw。git

  1. require 'rubygems'  
  2. require 'RMagick'  
  3. img =  Magick::Image.read('image/rails.png').first  
  4. width = img.columns  
  5. height = img.rows  
  6. thumb = img.resize(width * 0.8, height * 0.8)  
  7. thumb.write('image/rails_thumb_1.png') {self.quality = 50} #compress壓縮大小  

 

    1. require 'rubygems'  
    2. require 'RMagick'  
    3. img =  Magick::Image.read('image/rails.png').first #Image.read返回一個數組(由於有時候咱們會read一個GIF動畫,那麼GIF動畫的每一幀會存到這個數組裏,真是周到)  
    4. width, height = 100, 100  
    5. thumb = img.resize(width, height)   
    6. thumb.write('image/rails_thumb.png'

給圖片加水印(中文的)

首先給文件增長必要的gem引入github

require 'rubygems'
require 'RMagick'

 給圖片嵌入中文文字web

def t1     
  img=Magick::Image.read('path\read_image1.jpg').first    #圖片路徑,用相對路徑便可,相對於public來講的         
  my_text="\251 這是黑馬的標緻"        
  copyright=Magick::Draw.new  
  copyright.annotate(img,0,0,3,18,my_text) do  #可設字的位置   
    self.gravity = Magick::CenterGravity   
    self.font='public\images\simsun.ttc'  #這地方必須使用中文字庫,才能打中文到圖片上。在windows中c:\windows\fonts\simsun.ttc拷到項目的public\images目錄下就能夠隨着項目使用了。另外注意:筆者是使用utf-8字符集來編輯源文件的,若是你不是,請在程序中對漢字轉換編碼爲utf-8
    self.pointsize=96  #字體大小   
    self.font_weight=Magick::BoldWeight   
    self.fill='red'                    #字的顏色   
    self.gravity=Magick::SouthEastGravity   
    self.stroke = "none"  
  end  
  img=img.raise  #浮雕效果          
  img.write('path\img')  
end  
def test_photo    
  img=Magick::Image.read('public\photo\big_image\1\1119218437.jpg').first   
  img2=Magick::Image.read('public\photo\big_image\2\DSCN4991-thumb.jpg').first #版權圖片   
  my_text="\251 黑馬的標緻"  
  img.composite!(img2, -0,-0, Magick::CopyCompositeOp) #圖片疊加 ,CopyCompositeOp是composite的運算之一,還有不少運算方法,實現各類效果,能夠在官網找到 http://www.imagemagick.org/RMagick/doc/constants.html#CompositeOperator
  copyright=Magick::Draw.new  
  copyright.annotate(img,0,0,3,18,my_text) do  #可設字的位置   
    self.gravity = Magick::CenterGravity   
    self.font='public\images\simsun.ttc'     
    self.pointsize=96  #字體大小   
    self.font_weight=Magick::BoldWeight   
    self.fill='red'                    #字的顏色   
    self.gravity=Magick::SouthEastGravity   
    self.stroke = "none"  
  end  
  img=img.raise  #浮雕效果   
  mark.rotate!(-90)  #可旋轉
  img = img.watermark(mark, 0.15, 0, Magick::EastGravity)  #0.15是透明度 
  img.write('public\photo\big_image\1\1119218437-image1_bak.jpg')   
end 

文字特效

Rows = 60  
Cols = 250  
Text = 'Ruby rocks!'  
anim = Magick::ImageList.new  
ex = Magick::Image.new(Cols, Rows)  
text = Magick::Draw.new  
text.gravity = Magick::CenterGravity  
text.pointsize = 36  
text.font_weight = Magick::BoldWeight  
text.font_style = Magick::ItalicStyle  
text.stroke = 'transparent'  
text.annotate(ex, 0, 0, 2, 2, Text) {  
  self.fill = 'gray60'  
}  
anim << ex.copy  
ex = ex.blur_image(0, 3)  
anim << ex.copy  
text.annotate(ex, 0, 0, -1, -1, Text) {  
  self.fill = 'maroon'  
}  
anim << ex.copy  
anim.delay = 100  
anim.cur_image.delay = 300  
anim.iterations = 0  
anim.write('shadow.gif')  
exit  

縮略圖

[1] pry(main)> require "RMagick"
# => true
[2] pry(main)> img=Magick::Image.read('pic7.jpg').first 
# => pic7.jpg JPEG 600x800 600x800+0+0 DirectClass 8-bit 121kb
[3] pry(main)> c,r=img.columns,img.rows
# => [600, 800]
[4] pry(main)> l=c>r ? c : r
# => 800
[5] pry(main)> f=640.0/l
# => 0.8
[6] pry(main)> t1=img.thumbnail(f)
# => pic7.jpg JPEG 600x800=>480x640 DirectClass 8-bit
[7] pry(main)> t1.write('t1.jpg')
# => pic7.jpg=>t1.jpg JPEG 600x800=>480x640 DirectClass 8-bit 84kb
[8] pry(main)> img=nil
# => nil
相關文章
相關標籤/搜索