Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Draw Text in the Image
- - By Andrek Date 2012-03-03 15:10
You can draw a text on an image? example:

@image =, 'media/image.png')
@image.draw_text(0, 0, 'Name: Orc')

I need that merge text pixels to the pixels of the image. Please is very important to me, thank you very much.
Parent - - By lol_o2 Date 2012-03-03 18:50
Make an image from text and splice it with TexPlay. Example:

@image =, 'image.png')
@text = Image.from_text(@window, "Some text", font, 10)
@image.splice(@text, 0, 0)
Parent - - By Andrek Date 2012-03-04 11:40
That's what I need, but the text has a black background, is there anyway to remove it? I would use this:

@text = Image.from_text(@window, "Some text", font, 10)
for x in 0..@text.width
  for y in 0..@text.height
    color = @text.get_pixel(x, y)
    if color == [0, 0, 0, 1]
      @text.set_pixel(x, y, [0, 0, 0, 0]

Note: Untested code

also need to change the text color, is it possible? Thank you very much :D.
Parent - - By lol_o2 Date 2012-03-04 13:26
Add :chroma_key => [0,0,0,1] in .splice
Parent - By jlnr (dev) Date 2012-03-04 16:06 Edited 2012-03-05 01:08
There will be semitransparent pixels since text is rendered with anti-aliasing. Also it /should/ be [1, 1, 1, 1] but I don't know if this matters.

Is there a method to add or blend the text onto the image? That would probably be more reliable.
Parent - - By Andrek Date 2012-03-05 13:38 Edited 2012-03-05 13:56
The black background does not delete :S.

And how to change text color?.


This is the method:

def draw_text(x, y, string)
  text = Gosu::Image.from_text(@window, string, "Arial", 18)
  @image.splice(text, x, y, :chroma_key => [0, 0, 0, 1])
Parent - - By lol_o2 Date 2012-03-05 14:27
Ok, I don't know how to splice it :/
But, if you want to label sprite, can't you just draw a text above it? Or make Image.from_text and draw?

And if it REALLY need to be like that, you can use Window.record method. Looks like that:

@imgtxt = $window.record(width, height) do
  @text = Image_from_text(...

And then just draw @imgtxt
Parent - By RavensKrag Date 2012-03-06 15:54
Indeed, this seems like a better way to do it.

Personally, all of my gameobjects have #update and #draw, so I would just draw the sprite, and draw the label, in the #draw for the gameobject.  This provides a similar organization to the code.

It should be noted that doing it the way you (Andrek) want to do it will introduce additional overhead, as sprites with the same image, but different labels will not be able to share the same Gosu::Image for the sprite.
Parent - By jlnr (dev) Date 2012-03-06 04:26
As a quick fix you can try [1, 1, 1, 1] as the chroma key, it should look ugly but generally work.

If you want to use record just be aware that you have to keep a reference to the original image around too, otherwise the macro will not be rendered properly. (Oops...this sounds like something Gosu should take care of.)
Parent - - By jlnr (dev) Date 2012-03-04 07:34
BTW: If you are trying to recreate RPG Maker XP with Gosu, you should look around the boards, there have been various efforts to do just that :)
Parent - - By Andrek Date 2012-03-04 12:19
I'm not going to recreate the code, I will imitate the code :D. I'll remember but I have done a lot of code:

@sprite = # => Make
@sprite.x = x # => Make
@sprite.y = y # => Make
@sprite.z = z # => Make
@sprite.ox = ox # => Make
@sprite.oy = oy # => Make
@sprite.visible = visible # => Make
@sprite.zoom_x = zoom_x # => Make
@sprite.zoom_y = zoom_y # => Make
@sprite.angle = angle # => Make
@sprite.tone =, green, blue[, alpha = 255]) # => Make
@sprite.bitmap =, height) or # => Make
@sprite.bitmap.set_pixel(x, y, color) # => Make
@sprite.bitmap.get_pixel(x, y) # => Make
@sprite.bitmap.set_rect(x, y, width, height, color, fill) or @sprite.bitmap.set_rect(, y, width, height), color, fill) # => Make
@sprite.bitmap.src_rect =, y, width, height) # => Make
@sprite.bitmap.blt(x, y,'image.png')[,, y, width, height) =, 0, bitmap.width, bitmap.height)]) # => Make
@sprite.bitmap.clear # => Make
Keyboard.trigger?(key) # => Make
Keyboard.trigger_release?(key) # => Make # => Make
Mouse.trigger?(key) # => Make
Mouse.trigger_release?(key) # => Make # => Make
Parent - By RavensKrag Date 2012-03-06 16:09
With all due respect, that's not really that much code, as the first half of that list can be accomplished with a one line command.

It should be noted that I do not think that texplay uses a blit operation for merging images.  If you use your #blit a lot, then performance will suffer.  Trust me, I've tried it.

As I suspected, this notion of bliiting to the screen is similar to Pygame/Rubygame.  This is not how Gosu is structured, as Gosu has an OpenGL backend (someone correct me if I'm mistaken as to the viability of blitting in OpenGL/Gosu).  If you enjoy that workflow, I would suggest checking out Rubygame.  However, I consider Gosu to be superior because you have a little more abstraction.

oh, a note about Ruby notation:
Typically, when discussing methods, denotes a class method, while Object#foo denotes an instance method.
Parent - - By Donna111 Date 2013-10-28 09:13
Hi there
I am tring to draw text in the image using this code:
using System.IO;
using System.Drawing.Printing;
using RasterEdge.Imaging;
using RasterEdge.Imaging.Processing;
using RasterEdge.Imaging.Drawing.Image.Text;

RasterEdgeImaging Text = new RasterEdgeImaging();

Bitmap LoadImage = new Bitmap("C:\\1.jpg");
Graphic Text = Graphics.FromImage(LoadImage);
Font DrawFont = new Font("Arial", 16, FontStyle.Regular);
SolidBrush DrawBrush = new SolidBrush(Color.Red);
PointF DrawPoint = new PointF(10.0F, 30.0F);
Text.Text Drawing on Image = ("", DrawFont, DrawBrush, DrawPoint);


But i failed.What's wrong?Thanks for any suggestion.
Parent - By Spooner Date 2013-10-28 09:52
Your main problem is that isn't using the Ruby language or the Gosu library.
Up Topic Gosu / Gosu Exchange / Draw Text in the Image

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill