# Display Text as Image

**URL:** <https://rubytalk.org/t/display-text-as-image/66622>\
**Category:** ruby-talk\
**Created:** [4 November 2012 16:17 UTC](https://rubytalk.org/t/display-text-as-image/66622 "2012-11-04T16:17:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 November 2012 16:17 UTC](https://rubytalk.org/t/display-text-as-image/66622/1 "2012-11-04T16:17:13Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.size = "2000"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.height=1000  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.pointsize = 20  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.font = "Tahoma"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

img[0].display

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [4 November 2012 20:46 UTC](https://rubytalk.org/t/display-text-as-image/66622/2 "2012-11-04T20:46:29Z")

</div>

Quoting Adriana Mikolaskova (lists@ruby-forum.com):

> 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  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.size = "2000"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.height=1000  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.pointsize = 20  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.font = "Tahoma"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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)   
&nbsp;&nbsp;im=Magick::Image::new(x,y)  
&nbsp;&nbsp;im.import\_pixels(0,0,x,y,'RGB',pixels)  
&nbsp;&nbsp;im  
end

The string must be made up of x\*y\*3 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)  
&nbsp;&nbsp;im=Magick::Image::new(x,y) do  
&nbsp;&nbsp;&nbsp;&nbsp;self.background\_color=backg\_col  
&nbsp;&nbsp;end   
&nbsp;&nbsp;gc=Magick::Draw::new()  
&nbsp;&nbsp;gc.annotate(im,x,y,0,0,text) do  
&nbsp;&nbsp;&nbsp;&nbsp;self.font\_family=font  
&nbsp;&nbsp;&nbsp;&nbsp;self.fill=foreg\_col  
&nbsp;&nbsp;&nbsp;&nbsp;self.stroke=foreg\_col  
&nbsp;&nbsp;&nbsp;&nbsp;self.pointsize=pointsize  
&nbsp;&nbsp;&nbsp;&nbsp;self.gravity=Magick::CenterGravity  
&nbsp;&nbsp;end  
&nbsp;&nbsp;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](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

> **···**
>
> Subject: Display Text as Image  
> &nbsp;&nbsp;Date: Mon 05 Nov 12 01:17:13AM +0900
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [4 November 2012 20:57 UTC](https://rubytalk.org/t/display-text-as-image/66622/3 "2012-11-04T20:57:03Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 November 2012 20:51 UTC](https://rubytalk.org/t/display-text-as-image/66622/4 "2012-11-05T20:51:21Z")

</div>

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)  
> > &nbsp;&nbsp;im=Magick::Image::new(x,y) do  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.background\_color=backg\_col  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;gc=Magick::Draw::new()  
> > &nbsp;&nbsp;gc.annotate(im,x,y,0,0,text) do  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.font\_family=font  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.fill=foreg\_col  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.stroke=foreg\_col  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.pointsize=pointsize  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.gravity=Magick::CenterGravity  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;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
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [6 November 2012 07:10 UTC](https://rubytalk.org/t/display-text-as-image/66622/5 "2012-11-06T07:10:20Z")

</div>

Quoting Adriana Mikolaskova (lists@ruby-forum.com):

> 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

> **···**
>
> Subject: Re: Display Text as Image  
> &nbsp;&nbsp;Date: Tue 06 Nov 12 05:51:21AM +0900
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
