Display Text as Image

Hello,

I have a String, which I want to display as a bitmap image. What is the
best way to do this?

I tried f.e. the code below (RMagick), but the image displayed did not
contain the whole Text I stored in the variable myText. What can I do to
get the whole text displayed in an image? Or is there another, better
approach to do this?

Best Regards

Adriana

img = Image.read(“caption:”+myText) do
self.size = “2000”
self.height=1000
self.pointsize = 20
self.font = “Tahoma”

   end

img[0].display

Subject: Display Text as Image
Date: Mon 05 Nov 12 01:17:13AM +0900

Quoting Adriana Mikolaskova ([email protected]):

Best Regards
end
I am not sure I understand what you would like to obtain.

Here is the way to create a RMagick Image object, composed of given
pixels
passed in a string:

def make_image(x,y,pixels)
im=Magick::Image::new(x,y)
im.import_pixels(0,0,x,y,‘RGB’,pixels)
im
end

The string must be made up of xy3 bytes - the RGB values for each
pixel.

But from your example, it seems that you want to make an image which
includes a given message (characters to be read). This requires
something more complex:

def make_image_with_text(x,y,backg_col,foreg_col,text,font,pointsize)
im=Magick::Image::new(x,y) do
self.background_color=backg_col
end
gc=Magick::Draw::new()
gc.annotate(im,x,y,0,0,text) do
self.font_family=font
self.fill=foreg_col
self.stroke=foreg_col
self.pointsize=pointsize
self.gravity=Magick::CenterGravity
end
im
end

To have a 2000x1000 black image with “This is a test” written in
20point Tahoma in red in it you should call the above as follows:

im=make_image_with_text(2000,1000,‘black’,‘red’,‘This is a
test’,‘Tahoma’,20)
im.display

Of course you can do the two things in sequence - first draw some
pixels, and then superimpose some text…

Have a look at

http://studio.imagemagick.org/RMagick/doc/draw.html#annotate

Remember to destroy Image objects after using them, with

im.destroy!

Otherwise they are not garbage-collected, and if you generate many
images you soon end up with a full ram…

Carlo

Thank you very much for the detailled description and the code!!!it was
the second I’m looking for!

Hello,

your code worked fine for me, just one little problem occured.
When there are no linebreaks in the text, all the text is displayed on
one line (that means invisible).

I found different solutions, just wanted to ask here: is it really
necassary to figure out, how many characters a image-with can hold and
insert the line-breaks in the text? Or did I miss some easier method for
inserting line-breaks at the end of the image-width?

Best regards

Adriana

def make_image_with_text(x,y,backg_col,foreg_col,text,font,pointsize)
im=Magick::Image::new(x,y) do
self.background_color=backg_col
end
gc=Magick::Draw::new()
gc.annotate(im,x,y,0,0,text) do
self.font_family=font
self.fill=foreg_col
self.stroke=foreg_col
self.pointsize=pointsize
self.gravity=Magick::CenterGravity
end
im
end

To have a 2000x1000 black image with “This is a test” written in
20point Tahoma in red in it you should call the above as follows:

im=make_image_with_text(2000,1000,‘black’,‘red’,‘This is a
test’,‘Tahoma’,20)
im.display

O

Subject: Re: Display Text as Image
Date: Tue 06 Nov 12 05:51:21AM +0900

Quoting Adriana Mikolaskova ([email protected]):

your code worked fine for me, just one little problem occured.
When there are no linebreaks in the text, all the text is displayed on
one line (that means invisible).

I found different solutions, just wanted to ask here: is it really
necassary to figure out, how many characters a image-with can hold and
insert the line-breaks in the text? Or did I miss some easier method for
inserting line-breaks at the end of the image-width?

From what I know, no. Note that, for fonts that are not monospaced, it
is not simply a question of how many characters, because glyphs may
have different widths.

You can split your phrase in words, add one word at a time, measure
the width (with get_multiline_type_metrics) and insert a newline
before the word that would overflow. Not very difficult, but as far as
I know there is nothing pre-made in Magick.

Carlo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs