Rmagick在图像中写文字

时间:2010-09-09 09:53:35

标签: ruby rmagick

有人可以请我花一些代码,在图片的底部添加文字吗? 我想使用Rmagick,但我也可以使用其他工具。

2 个答案:

答案 0 :(得分:14)

我也发现了这一点,它对我来说很完美。

require 'RMagick'
include Magick

#Dimisions based on an image 3072x2048

unless ARGV[0] and File.exists?(ARGV[0])
  puts "\n\n\nYou need to specify a filename:  watermark.rb <filename>\n\n\n"
  exit
end

img = Image.read(ARGV[0]).first
new_img = "wm_#{ARGV[0]}"

if File.exists?(new_img)
  puts "Image already exists.  Unable to create file."
  exit
end

watermark = Image.new(600, 50)

watermark_text = Draw.new
watermark_text.annotate(watermark, 0,0,0,0, "foo bar bla") do
  watermark_text.gravity = CenterGravity
  self.pointsize = 50
  self.font_family = "Arial"
  self.font_weight = BoldWeight
  self.stroke = "none"
end

watermark.rotate!(45)
watermark = watermark.shade(true, 310, 30)
img.composite!(watermark, SouthWestGravity, HardLightCompositeOp)               #Bottom-Left Marking
watermark.rotate!(-90)
img.composite!(watermark, NorthWestGravity, HardLightCompositeOp)               #Top-Left Marking
watermark.rotate!(90)
img.composite!(watermark, NorthEastGravity, HardLightCompositeOp)               #Top-Right Marking
watermark.rotate!(-90)
img.composite!(watermark, SouthEastGravity, HardLightCompositeOp)               #Bottom-Right Marking

puts "Writing #{new_img}"
img.write new_img

答案 1 :(得分:5)

请查看RMagick docs