Can't get gravity to work with RMagick and 'caption'

Posted by mph on Stack Overflow See other posts from Stack Overflow or by mph
Published on 2009-12-09T03:49:53Z Indexed on 2010/12/22 16:54 UTC
Read the original article Hit count: 238

Filed under:
|
|

I'm using RMagick 2.12.2 with ImageMagick 6.5.6-10 on Snow Leopard. I'm trying to put captions on a collection of photos, and I'm getting the caption to work (i.e. it appears on the image), but I can't get the gravity parameter to work correctly.

No matter what I set it to, I end up with some variation on NorthGravity.

For instance: Setting it to SouthWestGravity gives me NorthWestGravity. Setting it to SouthEastGravity gives me NorthEastGravity. Setting it to CenterGravity gives me NorthGravity.

In other words, I can't get the caption to come down off the top of the image.

I'd consider using "annotate," but I need "caption" so the lengthy caption text for each image will wrap.

What am I doing wrong?

Here's the code:

#!/usr/bin/env ruby

require "rubygems"
require "yaml"
require "RMagick"
include Magick

base_dir = "/Users/mike/Desktop/caption_test"

photo_log = File.open("#{base_dir}/photo_log.yaml" )

YAML::load_documents(photo_log) do |doc|

  caption = doc["photo-caption"]
  filename = doc["file"]

  canvas = ImageList.new.from_blob(open("#{base_dir}/#{filename}") { |f| f.read } )

  canvas << Magick::Image.read("caption:#{caption}") { 
  self.gravity = SouthWestGravity
  self.size = "#{canvas.first.columns}"
  self.font = "Helvetica Neue"
  self.pointsize = 12
  self.background_color = "#fff"
 }.first

 canvas.flatten_images.write("#{base_dir}/images/#{filename}")

end

© Stack Overflow or respective owner

Related posts about ruby

Related posts about imagemagick