Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmagick write text in Images

Tags:

ruby

rmagick

Can someone please spend me some line of codes, that add a text at the bottom of an image? I want to use Rmagick but I'm open for other tools also.

like image 580
funkycottleti Avatar asked Sep 09 '10 09:09

funkycottleti


2 Answers

I found this also and it works for me perfect.

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
like image 136
funkycottleti Avatar answered Sep 28 '22 09:09

funkycottleti


Have look at the RMagick docs.

like image 33
hellvinz Avatar answered Sep 28 '22 08:09

hellvinz