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
self.size = "2000"
self.height=1000
self.pointsize = 20
self.font = "Tahoma"
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 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)
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
···
Subject: Display Text as Image
Date: Mon 05 Nov 12 01:17:13AM +0900
--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)