1
$\begingroup$

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?

$\endgroup$
5
  • 2
    $\begingroup$ use the option LineIndent -> 0 in Style. Default value is 1.. $\endgroup$
    – kglr
    Dec 24, 2018 at 11:13
  • $\begingroup$ @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. $\endgroup$
    – mrz
    Dec 24, 2018 at 11:41
  • 2
    $\begingroup$ mrz, to get a single line use the option LineBreakWithin -> False in Style. $\endgroup$
    – kglr
    Dec 24, 2018 at 11:50
  • $\begingroup$ @kglr: Merry Christmas and thank you for your help. $\endgroup$
    – mrz
    Dec 24, 2018 at 22:14
  • $\begingroup$ mrz, Merry Christmas to you too. $\endgroup$
    – kglr
    Dec 24, 2018 at 22:37

1 Answer 1

1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.