1

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?

ArgentoSapiens
  • 7,780
  • 1
  • 32
  • 49
  • Why not Style[Text[ "now is the time for all good men to come to the aid of their country"], FontSize -> 20]//Rasterize ? – Rod Jul 11 '13 at 18:30
  • 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. – Mr.Wizard Jul 11 '13 at 18:52
  • 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}] – m_goldberg Jul 11 '13 at 18:58
  • @m_goldberg I believe this question has been asked before; can you help me find it? – Mr.Wizard Jul 11 '13 at 19:42
  • @Mr.Wizard. I wouldn't be surprised if it were so, but a half=hour of searching didn't turn up anything. Sorry. – m_goldberg Jul 11 '13 at 20:21
  • @m_goldberg Sincere thanks for your efforts, and sorry for wasting your time. – Mr.Wizard Jul 11 '13 at 20:23
  • @Mr.Wizard Related?: http://mathematica.stackexchange.com/questions/15763/textcell-going-awry-due-to-imageresolution-in-mathematica-7 – Michael E2 Jul 13 '13 at 23:14
  • @MichaelE2 That's quite possibly what I was thinking of, since I replied to it. Thank you. – Mr.Wizard Jul 14 '13 at 02:15

2 Answers2

5

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}]
DavidC
  • 16,724
  • 1
  • 42
  • 94
1

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

george2079
  • 38,913
  • 1
  • 43
  • 110