1
$\begingroup$

I want to create a long, skinny Image containing only text.

This makes a square image of the default size.

Image@Graphics[
 {
  FontSize->20,
  Text["now is the time for all good men to come to the aid of their country"]
 }
]

enter image description here

So I prescribe the dimensions I want:

Image@Graphics[
 {
  FontSize->20,
  Text["now is the time for all good men to come to the aid of their country"]
 },
 ImageSize->{1000,30}
]

enter image description here

and the result is cropped well inside the ImageSize so some of the text is missing. But the Graphics alone is appearing correctly.

Graphics[
 {
  FontSize->20,
  Text["now is the time for all good men to come to the aid of their country"]
 },
 ImageSize->{1000,30}
]

enter image description here

Why does Image mysteriously crop the input?

$\endgroup$
8
  • $\begingroup$ Why not Style[Text[ "now is the time for all good men to come to the aid of their country"], FontSize -> 20]//Rasterize ? $\endgroup$
    – Rod
    Jul 11, 2013 at 18:30
  • $\begingroup$ I believe this issue has been covered before but I cannot find the question. Please help me look for it. @RodLm : Probably because he has other Graphics primitives in practice. $\endgroup$
    – Mr.Wizard
    Jul 11, 2013 at 18:52
  • $\begingroup$ You need to apply the ImageSize to Image, not Graphics. Try Image[Graphics[Text[Style["now is the time for all good men to come to the aid of their country", 20]]], ImageSize -> {600, 30}] $\endgroup$
    – m_goldberg
    Jul 11, 2013 at 18:58
  • $\begingroup$ @m_goldberg I believe this question has been asked before; can you help me find it? $\endgroup$
    – Mr.Wizard
    Jul 11, 2013 at 19:42
  • $\begingroup$ @Mr.Wizard. I wouldn't be surprised if it were so, but a half=hour of searching didn't turn up anything. Sorry. $\endgroup$
    – m_goldberg
    Jul 11, 2013 at 20:21

2 Answers 2

5
$\begingroup$

Image defaults to a certain size, but can be adjusted with its own ImageSize.

g=Graphics[{FontSize -> 20, Text["now is the time for all good men to come to the aid of their country"]};
Image[g, ImageSize -> {1000, 30}]
$\endgroup$
1
  • $\begingroup$ it seems you need to manually work out the aspect ratio..? $\endgroup$
    – george2079
    Jul 11, 2013 at 23:56
1
$\begingroup$

a bit of tweaking..

rastertext[ht_] := Function[{s}, Module[{},
          (*ht=approx pixel ht of M*)
   ImageCrop@
     Image[Graphics[{{FontSize -> 1.5 ht, Text[s]}}], 
                ImageSize -> 5  ht  { StringLength[s] , 1}]]]

the trick here is to scale the fontsize with the desided height, give ample room in the imagesize, then crop the result

z1 = rastertext[18]@(StringJoin @@ Table["M", {20}] ) 
z2 = rastertext[18]@(StringJoin @@ Table["M", {5}] ) 
z3 = rastertext[18]@(StringJoin @@ Table["M", {1}] )

enter image description here

enter image description here

enter image description here

verify we got the height right

ImageDimensions /@ {z1, z2, z3}

(* {{480, 18}, {120, 18}, {24, 18}} *)

rastertext[18]@"now is the time for all good men to come to the aid of their country"

enter image description here

for fun..

words = rastertext[18] /@  
       StringSplit[
         "now is the time for all good men to come to the aid of their country"];
p = {0, 0};
Graphics[Last@Last@Reap[
           Do[d = ImageDimensions[w]; Sow[Inset[w, p, {0, 0}, d]]; 
           p += {d[[1]] + 15, 0}; 
           If[p[[1]] > 300, p = {0, p[[2]] - 30}], {w, words}]], 
           PlotRange -> {{0, 350}, {-60, 25}}]

enter image description here

..need more tweaking to get the baselines aligned..

rastertext[ht_] := Function[{s}, Module[{},
  (*ht=approx pixel ht of M*)
  pad = If[ Length[StringPosition[s, {"g", "q", "p", "y", "j"}]] == 0 , ht/3, 0];
  ImagePad[ ImageCrop@
    Image[Graphics[{{FontSize -> 1.5 ht, Text[s]}}], 
        ImageSize -> 5  ht  { StringLength[s] , 1}], {{0, 0}, {pad, 0}},White]]]

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.