# Adding text of variable size to the corner of an image so that it always fits

I have an image for example:

img = Import[
"http://ultraimg.com/images/2016/07/29/Simplest-Responsive-jQuery-\
Image-Lightbox-Plugin-simple-lightbox.jpg"]


I'm creating a text graphics object:

string = "a test string";
gObj = Graphics[
Text[Style[string, FontSize -> 14, FontFamily -> "Arial", Bold,
White, Background -> Black]]];


and add it to the image in the left bottom corner:

picFinal = ImageCompose[img, gObj, {60, 10}]


Now when I use a longer string:

string = "a test string that is a bit longer ";


this results in the string not fitting. But I need to add strings with variable sizes to the left bottom corner as well. How can I adapt the position of my string such that it always has a specified distance from the left bottom corner and adapt dynamically to the size of the string?

I think the problem here is the automatic rasterisation of the text. When you give a Graphics object to an image based function, it automatically converts it to an image.

ImageAssemble[{{
Graphics[Text@"a test string", Background -> LightGray],
Graphics[Text@"a test string that is a bit longer", Background -> LightGreen]
}}]


Which is fine, but with the big border you lose control of where the text starts. However if you explicitly Rasterize the text, without making it into Graphics, it's more compact and predictable:

gObj = Rasterize[
Text[Style[string, FontSize -> 14, FontFamily -> "Arial", Bold,
White, Background -> Black]], Background -> None]


Then you can use ImageCompose parameters to good effect to always get it in the left bottom:

ImageCompose[img, gObj, {5, 0}, {Left, Bottom}]


• I see, thank you! Oct 25 '16 at 10:43