7
$\begingroup$

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@
  Graphics[ 
   Text[Style["W", 24, FontFamily -> "Helvetica", 
     TextAlignment -> Center, Antialiasing -> True], {0, 0}], 
   ImagePadding -> 0, ImageSize -> 24]

imZ = Rasterize@
  Graphics[ 
   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

$\endgroup$
2
$\begingroup$

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];
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Putterboy Aug 24 '14 at 9:44
  • $\begingroup$ @user16069 Does this work as you desire? ImageCrop @ Graphics[Text[Style[#, 24, FontFamily -> "Helvetica", Antialiasing -> True], {#2, 0}] & ~MapThread~ {{"W", "Z"}, {0, 0.07}}] $\endgroup$ – Mr.Wizard Aug 24 '14 at 18:55
  • $\begingroup$ 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. $\endgroup$ – Putterboy Aug 24 '14 at 19:47
6
$\begingroup$

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.

EDIT:

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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