Forum Archive

Text on Layers

NoneNone94

hi

i was trying to blend a text with an image and use it as an image of a layer. i opened the background image, wrote a text on it using ImageDraw.text and loaded it as layer's image with load_pil_image.

but it seems that quality of the text is poorer that a text directly drawn on the scene using scene.text.

1- is there a solution for that?

2- while i was searching for a solution i found the TextLayer command in the Cascade example, for which no documentation seems to be available. can i use this to solve my problem?

thnx

omz

When you use ImageDraw on a retina device, you might need to double the width and the height of your image to make it looks crisp. The scene module uses a "point" coordinate system (on retina 1 point = 2 pixels), while ImageDraw uses normal pixels.

The TextLayer class is currently undocumented, but pretty simple to use. It's basically just a layer that uses the result from render_text() as its image. There isn't much more to it than what you see in the examples, in fact, this is the entire source code of the class:

class TextLayer (Layer):
  def __init__(self, text, font, font_size):
    Layer.__init__(self)
    img, size = render_text(text, font, font_size)
    self.image = img
    self.frame = Rect(0, 0, size.w, size.h)

To combine it with an image, you could add a TextLayer as a sublayer of an image layer, using the add_layer() method.

omz

You can simply set the tint property of the TextLayer to set it to a different color, e.g. for red text:

text_layer = ...
text_layer.tint = Color(1, 0, 0)