2
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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]
}}]

1

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]

2

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

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

3

$\endgroup$
1
  • $\begingroup$ I see, thank you! $\endgroup$
    – holistic
    Oct 25, 2016 at 10:43

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.