Watermarking Photos using Ruby

The task: take a source photo of any size and overlay transparent text atop it. This is useful for asserting to the world that the image is yours (which it sometimes is).

Before:

Mike Burns undressing after a long day of work.

After:

Mike Burns undressing after a long day of work, watermarked

First, some configurables:

IMAGE_FILE_NAME =  '/usr/home/mike/pictures/c4rt.jpg'
OVERLAY_FONT =  '/usr/X11R6/lib/X11/fontsbitstream-vera/Vera.ttf'
OVERLAY_ANGLE = 1
OVERLAY_STRING =  "Copyright #{Date.today.year} Mike Burns"

For this task I used the GD library. Step one, suck in the image:

i =  GD::Image.new_from_jpeg(IMAGE_FILE_NAME)

Step two: overlay the string:

i.stringTTF(GD::Image.trueColorAlpha(#cccccc, 64), OVERLAY_FONT, i.width * 0.05, OVERLAY_ANGLE, i.width / 4, i.height * 3/4, OVERLAY_STRING)

Step three: save the image:

File.open(IMAGE_FILE_NAME,'w') { |f| i.jpeg(f,100) }
Advertisements
%d bloggers like this: