Fun with RMagick

written by jared on June 19th, 2009 @ 01:58 PM

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;

  • I start by loading the image and storing the width and height.
  • Next, I create a black overlay, with 60% opacity, the width of the original image and 20 pixels high.
  • I apply the overlay to the original picture, using SouthGravity to place it centered on the bottom of the image.
  • Then, I create my drawing object, set the fill color, opacity, and font options.
  • I use the drawing object to place the text on the composite image, setting the baseline for the text 5 pixels above the bottom of the image, and setting the end of the text (right justified) 5 pixels in from the right border of the image.
  • Finally, I append the text "-final" to the filename, just before the extension, and save it back to the filesystem.

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.

Comments

  • IceskYsl on 22 Jun 02:39

    thanks,and i change some to this. 1. picture = Magick::Image.read(ARGV0) => picture = Magick::Image.read(ARGV0)[0]

    2. Helvetica is not copyright.font_family(‘Helvetica’) copyright.font_family(‘arial’)

  • Jared on 23 Jun 13:34

    Ah, good catch on the Image.read line. I had adapted this from a script that processed a directory instead of a single file, I must have overlooked the file load line.

    I think the font_family call may be platform dependent. On my Mac, the Helvetica family is installed, so that works for me.