Adding text to pictures with Rmagick and Rails.

Recently I needed to add some dynamically generated text to an image with Ruby on Rails. For some reason there do not seem to be many tutorials on how to work with RMagick so I thought I would share what little I now about it. Originally I looked at Image Science but it turns out that its very focused on simply resizing images. So for this I turned to RMagick. I was a little worried that the installation would be nightmarish but it turned out to be quite painless (at least on Ubuntu. I can’t imagine what it would be like on Windows. Probably bad.):

mike@sleepycat:~$ sudo aptitude install imagemagick librmagick-ruby libmagickwand-dev

Then install the gem:

mike@sleepycat:~$ sudo gem install rmagick

RMagick is a complex library that has tonnes of graphics functions and can transform images in many different ways.
It seems that RMagick works conceptually the same for both Vector graphics and bitmap images: create an image and add text objects to it or create a canvas and add vector objects to it (using Ruby Vector Graphics aka:RVG). So lets do that. So here is an image to start with:

To start playing with RMagick, you can stick this in one of your controllers:

require ‘RMagick’
include Magick

def lolcat

img =‘public/computer-cat.jpg’)
txt =

img.annotate(txt, 0,0,0,0, “In ur Railz, annotatin ur picz.”){
txt.gravity = Magick::SouthGravity
txt.pointsize = 25
txt.stroke = ‘#000000’
txt.fill = ‘#ffffff’
txt.font_weight = Magick::BoldWeight

img.format = ‘jpeg’
send_data img.to_blob, :stream => ‘false’, :filename => ‘test.jpg’, :type => ‘image/jpeg’, :disposition => ‘inline’


And here is the output of that script:

Although sticking that in a controller is a quick way to see the results of some tinkering and experimentation, this is the kind of stuff that should really be in a model. You would likely want to return img.to_blob from one of your model methods and then use send_data from there.

It’s pretty basic but it was all I needed to get my project working. RMagick does seem to be a little arcane. A lot of the things I tried along the way failed in pretty confusing ways and with pretty cryptic errors. While simple stuff like this is fine its going to take a lot more reading and fiddling to make me truly comfortable with RMagick. At least I have this written down for next time…


8 thoughts on “Adding text to pictures with Rmagick and Rails.”

  1. I’ve been trying to get rmagick running on Rails3 for a day now and failed on 2 different installations. Any pointers?

  2. Hey Mike! Thanks for this. I know this is a n00b question (but it could help others who view this form in the future). How would I make the text dynamic and based on user’s input?

    If I have a form, why can’t I just stick where “text” is in rMagick?

    Any tips would be great!

    1. Oops! My comment got formatted strangely. Let me rephrase. What should I insert where you wrote, “In ur Railz, annotatin ur picz” to append dynamic user-generated text onto the image?

      1. build a form, and send the user input value to your controller. In your controller you can write what mike did. Replace “In ur Railz, annotatin ur picz.” with the string that is passed in from the form.

  3. Thanks Song! A friend helped me figure it out. He showed me that if you put ‘model.formfieldname’ where the text is, where ‘formfieldname’ is whatever you named the form field with the user-input, that it works :D

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s