Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I attempt to test the mean graylevels of the alphabets "W" and "Z". I used the following code to create the images of them.

imW = Rasterize@
   Text[Style["W", 24, FontFamily -> "Helvetica", 
     TextAlignment -> Center, Antialiasing -> True], {0, 0}], 
   ImagePadding -> 0, ImageSize -> 24]

imZ = Rasterize@
   Text[Style["Z", 24, FontFamily -> "Helvetica", 
     TextAlignment -> Center, Antialiasing -> True], {0, 0}], 
   ImagePadding -> 0, ImageSize -> 24]

However, there are blank areas around the images. How can I get rid of them? Is there a trim function built in for this purpose?

enter image description here

I tried ImageCrop[imW ], however, the top and bottom blanks still left.

@kirma I did follow your suggestions, both increasing the ImagePadding to 20, and ImagePad[img, -BorderDimensions[img]]. Somehow, I still can't get rid of the top and bottom blanks. What I got was something like this:

enter image description here

share|improve this question
up vote 2 down vote accepted

Either of these appear to work on my system (v10, Windows):

img1 = ImageCrop@
   Graphics[Text @ Style["W", 24, FontFamily -> "Helvetica", Antialiasing -> True]];

img2 = ImageCrop@
   Rasterize@Style["W", 24, FontFamily -> "Helvetica", Antialiasing -> True];
share|improve this answer
Your codes are always clean and straight to the point! I would like to ask one more thing, what would be your code for the image when the "W" and "Z" overlapped each other? – Putterboy Aug 24 '14 at 9:44
@user16069 Does this work as you desire? ImageCrop @ Graphics[Text[Style[#, 24, FontFamily -> "Helvetica", Antialiasing -> True], {#2, 0}] & ~MapThread~ {{"W", "Z"}, {0, 0.07}}] – Mr.Wizard Aug 24 '14 at 18:55
Thanks a lot, I got exactly what I want now: ImageCrop@Graphics[ Text[ Style[#, 24, FontFamily -> "Helvetica", Antialiasing -> True]] & ~MapThread~ {{"W", "Z"} }] instead of my adoption of your previous answer: ImageCrop@Graphics[{Text@Style["W", 24, FontFamily -> "Helvetica", Antialiasing -> True], Text@Style["Z", 24, FontFamily -> "Helvetica", Antialiasing -> True]}] With your code I can obtain the overlapped images from a list of alphabets more easily now. – Putterboy Aug 24 '14 at 19:47

Use ImageCrop. It does exactly what you want, at least in this case... except in the case where "empty" space exists only on one side of the image, which it refuses to crop. To work around that, you can use ImagePadding or ImagePad to create extra padding, which allows ImageCrop to see removable padding on all sides.


Other, a bit less hacky alternative is ImagePad[img, -BorderDimensions[img]].

ImagePad[imW, -BorderDimensions[imW]]

enter image description here

ImagePad[imZ, -BorderDimensions[imZ]]

enter image description here

share|improve this answer

Your Answer


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.