As I have already done with python (in How to add a text to a picture using python) , I'm going to write a simple program which adds a text over an image.

The text I want to add will be a simple string and it will be semi-transparent.

 

To write such a program, as usual I'll use the RMagick library, take a look at the related posts to install ruby and the imageMagick libraries. 

 

To understand waht I want to do I want to show the original picture and the modified picture:

Original Picture:

gg

Final Picture:

 

 wm_gg

 

 

#

#   www.xappsoftware.com

#   text_over.rb

#   A tool to write a semi-transparent textover a picture.

#

require 'RMagick'

include Magick

 

img = Image.read(ARGV[0]).first

new_img = "Text_over_#{ARGV[0]}"

 

layer2 = Image.new(800, 150)

 

text_over = Draw.new

text_over.annotate(layer2, 0,0,0,0, "(c) xAppsoftware") do

    text_over.gravity = CenterGravity

    self.pointsize = 100

    self.font_family = "Ubuntu"

    self.stroke = "none"

end

 

layer2.rotate!(20)

layer2 = layer2.shade(true, 310, 30)

img.composite!(layer2, SouthWestGravity, HardLightCompositeOp)

img.write("#{new_img}")

 

 

The program makes use of the RMagick libraries.

At startup the program reads from the CLI arguments the name of the file we want to modify.

Then, it prepares a new image which will contain the text.

The text_over is a new draing containing the Text with the characteristics specified by gravity, pointsize, font_family and stroke.

the img.composite statement will merge the two images.

and the last line will save the merged image into a new with the new name. I named "text_over.rb" this program. So to launch it on the gg.png image I simply issue the following command:

$ ruby text_over.rb gg.png

 

Gg1