Forum: Ruby text to images using RMagick

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ray (Guest)
on 2006-03-14 22:44
(Received via mailing list)
Hi, I used this thread
(http://groups.google.com/group/comp.lang.ruby/brow...)
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." <removed_email_address@domain.invalid> wrote in message

news:Klpqe.1304$removed_email_address@domain.invalid...
> 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
Timothy H. (Guest)
on 2006-03-15 01:09
(Received via mailing list)
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
Ray (Guest)
on 2006-03-17 09:29
(Received via mailing list)
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
Ray (Guest)
on 2006-03-17 10:40
(Received via mailing list)
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
(Guest)
on 2006-03-17 15:19
(Received via mailing list)
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
http://www.simplesystems.org/RMagick/doc/rvgtext.html, with particular
attention to tspans:
http://www.simplesystems.org/RMagick/doc/rvgtspan.html

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!
Timothy H. (Guest)
on 2006-03-19 02:35
(Received via mailing list)
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]http://www.simplesystems.org/RMagick/doc/imusage.h...
[2]http://www.simplesystems.org/RMagick/doc/draw.html#text
[3]http://www.simplesystems.org/RMagick/doc/rvgtext.html
[4]http://www.simplesystems.org/RMagick/doc/draw.html#annotate
[5]http://www.cit.gu.edu.au/~anthony/graphics/imagick6/text/
Alex C. (Guest)
on 2006-03-20 22:51
(Received via mailing list)
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.
Timothy H. (Guest)
on 2006-03-21 00:54
(Received via mailing list)
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!
This topic is locked and can not be replied to.