Get a quote!

Fun with RMagick

Written by Jared Haworth on

As an avid photographer, I spend a lot of time tweaking my images to share them online. I use Adobe Lightroom for all my cataloging needs, but when it comes time to display the pictures on the internet, I like to add a bit of copyright information to try and ward off the most casual attempts to reuse my images without giving credit.

This is the sort of task that RMagick is incredibly well-suited to handle. Plus, it indulges my geeky side to manipulate images using Ruby code. Here’s a snippet from my copyright script:

require 'rubygems'
require "RMagick"
include Magick

# Picture Section
picture = Magick::Image.read(ARGV[0]).first
width,height = picture.columns, picture.rows

# Overlay Section
overlay = Magick::Image.new(width, 20) {
  self.background_color = "rgba(0,0,0,0.6)"
}

# Combine them!
  picture.composite!(overlay, SouthGravity, MultiplyCompositeOp)

# Text Section
copyright = Draw.new
copyright.fill('white')
copyright.fill_opacity(0.75)
  
copyright.pointsize(14)
copyright.font_family('Helvetica')
copyright.font_weight(LighterWeight)
copyright.font_style(Magick::ItalicStyle)
  
copyright.text_align(RightAlign)

#  Place the copyright onto the composite image 
copyright.text(width - 5,height - 5,"© 2009 Jared Haworth")
copyright.draw(picture)

out = ARGV[0].sub(/\./, "-final.")
puts "Writing #{out}"
picture.write(out)

The code is fairly straightforward;

The end result looks like this: Glowsticks

The RMagick documentation is a great help, and has tons of examples. I’ve used variations on this same code to create wet floor effects, drop shadows, and those neat, Polaroid-style images with the curly borders. And since it’s all Ruby code, you can use the File and Dir objects to iterate over an entire directory of images, performing drop dead simple batch processing.