Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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
show 3 more comments

2 Answers 2

up vote 5 down vote accepted

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}]
share|improve this answer
    
it seems you need to manually work out the aspect ratio..? –  george2079 Jul 11 '13 at 23:56
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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