Text to images using RMagick

Hi, I used this thread
(http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/c1e73e86ca02c6af/7868ea24e7069eea?#7868ea24e7069eea)
to get text wrapping working, and it’s working great.

The problem I can’t seem to crack is forcing linefeeds or newline
breaks. How do I do it in a string constructed as a “caption:”? Or is
there another way to create an image with automatically wrapping text
that would support this?

Ta, Ray

“Timothy H.” [email protected] wrote in message

news:[email protected]

Okay, I just uploaded a new version of RMagick, version 1.8.2. With this
version you can use this script:
require ‘RMagick’
include Magick

img = Image.read(“caption:My very long caption which should wrap at 200
pixels”) do
self.size = “200x”
self.pointsize = 20
self.font = “Tahoma”
end

Ray wrote:

img = Image.read(“caption:My very long caption which should wrap at 200
pixels”) do
self.size = “200x”
self.pointsize = 20
self.font = “Tahoma”
end

img[0].display

This works for me. You may need to upgrade your ImageMagick. I’m using
ImageMagick-6.2.6-3.

require ‘RMagick’

str = “caption:This is\na string\nwith embedded\nnewlines\n”

img = Magick::Image.read(str) {self.size = “200x200”; self.gravity =
Magick::CenterGravity}
img[0].display

Hi Tim, thanks for your quick response. I’m very new to this, Im on Mac
OSX 10.4.5, how do I check the version of ImageMagick installed?

Also, when I do get it working, can all text formatting (eg: bold,
italic, colours, etc) work with the caption parameter?

Ta, Ray

Tim, i just tried this:

Tiger:/opt/local/bin rails$ composite -version
Version: ImageMagick 6.2.4 02/24/06 Q16 http://www.imagemagick.org

So i guess I’m running ImageMagick 6.2.4???

Also, to clarify my question around text formatting, I need to set
formats in line (on particular words), not globally to the caption
parameter. Can it be done?

Cheers and happy St. Patrick’s Day, Ray

Ray wrote:

Hi Tim, thanks for your quick response. I’m very new to this, Im on Mac
OSX 10.4.5, how do I check the version of ImageMagick installed?

Also, when I do get it working, can all text formatting (eg: bold,
italic, colours, etc) work with the caption parameter?

Ta, Ray

Enter this command in irb:

irb -r RMagick
puts Magick::Long_version

The doc on the “caption” builtin format[1] says that you can use these
options with caption:

antialias
background_color
border_color
density
fill
font
gravity
pointsize

font_style and font_weight aren’t on the list, so you can’t specify
italic or bold with caption. You could specify a bold or italic font by
name instead. (This is a limitation of ImageMagick, not RMagick.) To
specify those kinds of attributes you’re going to have to use the
Draw#text method[2] or RVG::Text[3] or the Draw#annotate method[4]. The
annotate method will handle strings with embedded newlines, the other
two don’t.

Keep in mind that ImageMagick will not alter a font (the way Windows
slants a font to fake italics, for example) to match the attributes you
specify. It simply searches its list of fonts (which may not include all
the fonts you have on your system) to find the closest match and uses
that. If you ask for a bold Arial and it doesn’t have Arial Bold on its
list, it’ll just give you Arial. If you absolutely need a certain font,
the best thing to do is to specify the full path to the font. Anthony
Thyssen has a great set of ImageMagick examples, including one about
fonts[5]. The examples use the ImageMagick commands but it’s not hard to
map them into the RMagick API. (If you do need help translating the
commands into RMagick send me an email and I’ll see what I can do.)

[1]RMagick 1.15.0: ImageMagick/GraphicsMagick Conventions
[2]RMagick 1.15.0: class Draw
[3]RMagick 1.15.0: RVG Reference: RVG::Text Class
[4]RMagick 1.15.0: class Draw
[5]http://www.cit.gu.edu.au/~anthony/graphics/imagick6/text/

The rmagick docs look absolutely amazing

http://www.simplesystems.org/RMagick/doc/index.html

However right at the very start the doc basically
says “if you’re on windows then this wont work” and then
provides no example to show how to make it work.

=> Img.write(“foo.gif”) #for windows users please!

Finding out how to get started is usually the hardest part of learning
anything new.

Geez, thanks, heh.

Ray wrote:

Tim, i just tried this:

Tiger:/opt/local/bin rails$ composite -version
Version: ImageMagick 6.2.4 02/24/06 Q16 http://www.imagemagick.org

So i guess I’m running ImageMagick 6.2.4???

Yes, that’s right.

Also, to clarify my question around text formatting, I need to set
formats in line (on particular words), not globally to the caption
parameter. Can it be done?

No. ImageMagick is an image processing library so its support for text
is limited. If you absolutely have to do this using Ruby+ImageMagick
then you might have more luck with RMagick’s RVG class. It’ll still be
hard. While RVG does allow you to specify styling on individual words,
it doesn’t support embedded newlines so you’ll have to do the
positioning yourself. Check out RVG’s support for text at
RMagick 1.15.0: RVG Reference: RVG::Text Class, with particular
attention to tspans:
RMagick 1.15.0: RVG Reference: RVG::Tspan Class

Another approach would be to produce an EPS file, or a PDF (with
PDF::Writer) that contains the formatted text and then convert it to
the destination format with RMagick.

Cheers and happy St. Patrick’s Day, Ray

Thanks! You too!

Alex C. wrote:

Finding out how to get started is usually the hardest part of learning
anything new.

Thanks for the kind words, Alex! I guess I need to look to look at that
part of the doc again, because I really don’t want to mislead Windows
users. I guess you’re talking about this note:

MS Windows users note: The display method does not work on native MS
Windows. You must use an external viewer to view the images you create.

What I’m trying to say is that ImageList#display doesn’t work on
native MS Windows, not that Image#write doesn’t work.

To clarify the doc, the Image#write method works just fine on MS
Windows, as do all of the other RMagick methods, with the exception of
those methods that need X11. To be specific, the methods that rely on
X11 (and thus don’t work on MS Windows) are Image#display,
ImageList#display, ImageList#animate, and Image.capture. And, when I say
“MS Windows” I mean “native” MS Windows, not Cygwin. The difference is
that you can install X11 on Cygwin but not on “native” MS Windows.

Until I get the doc fixed up, you might be able to figure out the
confusing bits by looking at and running some of the example scripts
that ship with RMagick. As always, Ruby makes the code simple and easy
to understand.

Thanks for pointing out this deficiency in the doc. I’ll see what I can
do to remedy it!