1

What is the best solution to insert text at defined poition into image?

I saw this question/answer but it does not help me for my examples below.

I tried the following and find some strange behaviour:

image = Image[Array[0 &, {200, 400}]];

Example 1:

text = "This is a very long text";
textImage = 
  textImage = Rasterize[Style[text, FontFamily -> "Calibri", 30]];
composedImage = Show[image, textImage]

enter image description here

Example 2:

text = "This is a very long text This is a very long text This is a very long text This is a very long text";
textImage = 
  textImage = Rasterize[Style[text, FontFamily -> "Calibri", 30]];
composedImage = Show[image, textImage]

Here the text is cropped.

enter image description here

Example 3:

text = "This is a very long text This is a very long text This is a very long text This is a very long text This is a very long text";
textImage = 
  textImage = Rasterize[Style[text, FontFamily -> "Calibri", 30]];
composedImage = Show[image, textImage]

I don't understand why here the text is wrapped but not left aligned. It seems that when the text exceeds a certain length (probably depending on text size) this happens.

enter image description here


What would you propose to insert text into an Image?

mrz
  • 11,686
  • 2
  • 25
  • 81
  • 2
    use the option LineIndent -> 0 in Style. Default value is 1.. – kglr Dec 24 '18 at 11:13
  • @kglr: If I put LineIndent -> 0 in example 3 I get 2 rows now left aligned and wrapped. How can I have a single line although it is too long and not everything seen? In general: I believe that my way how I insert text is not the correct one. There must be a more elegant solution, where I also can set the position. – mrz Dec 24 '18 at 11:41
  • 2
    mrz, to get a single line use the option LineBreakWithin -> False in Style. – kglr Dec 24 '18 at 11:50
  • @kglr: Merry Christmas and thank you for your help. – mrz Dec 24 '18 at 22:14
  • mrz, Merry Christmas to you too. – kglr Dec 24 '18 at 22:37

1 Answers1

1

To supress line breaks you can use the option LineBreakWithin -> False in Style:

text = "This is a very long text This is a very long text This is a very long text This is a
  very long text This is a very long text";
textImage = Rasterize[Style[text, FontFamily -> "Calibri", 30, LineBreakWithin -> False]];
composedImage = Show[image, textImage]

enter image description here

To prevent indents use the option LineIndent -> 0:

textImage = Rasterize[Style[text, FontFamily -> "Calibri", 30, LineIndent -> 0]]
composedImage = Show[image, textImage]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896