安装rmagick gem
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:
or if you want to be on edge:
You also install it directly by doing:
|
使用RMagick,重要会用到两个对象:Image和Draw。
首先给文件增加必要的gem引入
1
2 |
require ‘rubygems‘ require ‘RMagick‘ |
给图片嵌入中文文字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
[ 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 |
「ruby」使用rmagick处理图像,布布扣,bubuko.com
原文:http://www.cnblogs.com/lizunicon/p/3675096.html