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 am trying to generate & Export[] Image with the below code, but as shown in the images I get some white border around my rectangle.

How could I export an images that stopes right at the edge of my Rectangle[] edges ?

enter image description here

c0 = {RGBColor[23/85, 29/255, 142/255], RGBColor[244/255, 1, 59/255], 
      RGBColor[1, 0, 32/85], RGBColor[18/85, 72/85, 197/255]}


Export[FileNameJoin[{Directory[], "DropBox", ToString[#] <> ".jpg"}],
Graphics[{EdgeForm[Thick],
 White, Rectangle[{0, 0}, {160, 90}],
 Flatten@({Flatten@(Table[
          RandomChoice[{GrayLevel[.15], c0[[#]]}], {3}] & /@ 
        Range[2, 4, 1]),
     MapThread[
      Function[{Xs, Ys},
       Rectangle[{Xs, Ys}, {Xs + 16, Ys + 9}]],
      {Flatten@Table[Range[0, 32, 16], {3}], 
       Flatten@(Table[#, {3}] & /@ 
          Range[63, 81, 9])}]}\[Transpose]),
 Black, Thick, Line[{{0, 63}, {160, 63}}]}, ImageSize -> 300]] & /@
 Range[100]
share|improve this question
1  
With png files, you can use Export with option Background->None; but with jpg images this trick does not work. –  kguler Feb 3 '12 at 3:37
add comment

4 Answers

up vote 17 down vote accepted

There's another, undocumented, approach, although I can't take credit for discovering this one. The solution you're probably looking for (in the sense that Brett Champion's solution seems to clip off a little too much at the edges) is the Method option for Graphics:

Method -> {"ShrinkWrap" -> True}

e.g. (graphics example from the documentation for Circle):

Graphics[
  Table[{Hue[t/20], Circle[{Cos[2 Pi t/20], Sin[2 Pi t/20]}, 1]}, {t, 20}], 
  Method -> {"ShrinkWrap" -> True}
]

Note that this has to be written as Method -> {"ShrinkWrap" -> True}. The form Method -> "ShrinkWrap" -> True might be expected to work, but it doesn't.

share|improve this answer
2  
+1, I didn't know "ShrinkWrap" worked in 2D graphics. –  Brett Champion Feb 3 '12 at 3:37
    
@Oleksandr, definetly does it, Thank You :-) –  500 Feb 3 '12 at 3:42
    
What is the reason for the Method option not being documented for Graphics -- meaning that its existence is documented but how to use it is not. –  Mike Honeychurch Feb 3 '12 at 3:50
2  
I don't know; I don't have any special insight into that kind of thing. I'm not a WRI employee; I just seem to be mentioning undocumented functionality a lot lately as I'm willing to talk about it whereas whereas WRI, usually, is not. –  Oleksandr R. Feb 3 '12 at 3:56
3  
"ShrinkWrap" works very well, but can take a bit of time to render. If you combine several shrinkwrapped graphics (e.g. via Inset or Grid inside a dynamic expression like Manipulate then you will start to notice the lag. –  Yves Klett Feb 7 '12 at 8:19
add comment

Set the following Graphics options:

PlotRangePadding -> None
ImagePadding -> None

If setting ImagePadding -> None is too aggressive, you can add back in fixed amounts of padding with something like ImagePadding -> 1 or the more general matrix form.

share|improve this answer
1  
Thank You but than now seems to eat the edge of the Rectangle Edges. Is there a neat way to fix, Or do I have to double the EdgeThickness[] to have only the inner part ? –  500 Feb 3 '12 at 3:22
add comment

If jpg is not essential so that you can use png instead, setting the background to None in Export works:

Export["tst" <> ToString[#] <> ".png", 
Graphics[{EdgeForm[Thick], White, Rectangle[{0, 0}, {160, 90}], 
 Flatten@({Flatten@(Table[
          RandomChoice[{GrayLevel[.15], c0[[#]]}], {3}] & /@ 
        Range[2, 4, 1]), 
     MapThread[
      Function[{Xs, Ys}, 
       Rectangle[{Xs, Ys}, {Xs + 16, Ys + 9}]], {Flatten@
        Table[Range[0, 32, 16], {3}], 
       Flatten@(Table[#, {3}] & /@ 
          Range[63, 81, 9])}]}\[Transpose]), Black, Thick, 
 Line[{{0, 63}, {160, 63}}]}, ImageSize -> 300], 
Background -> None] & /@ Range[10]

You get:

example png image

EDIT: To change the white rectangle to transparent, try

Export["xtst" <> ToString[#] <> ".png", 
Graphics[{EdgeForm[Thick], Opacity[0], 
 Rectangle[{0, 0}, {160, 90}], Opacity[1], 
 Flatten@({Flatten@(Table[
          RandomChoice[{GrayLevel[.15], c0[[#]]}], {3}] & /@ 
        Range[2, 4, 1]), 
     MapThread[
      Function[{Xs, Ys}, 
       Rectangle[{Xs, Ys}, {Xs + 16, Ys + 9}]], {Flatten@
        Table[Range[0, 32, 16], {3}], 
       Flatten@(Table[#, {3}] & /@ 
          Range[63, 81, 9])}]}\[Transpose]), Black, Thick, 
 Line[{{0, 63}, {160, 63}}]}, ImageSize -> 300], 
 Background -> None] & /@ Range[10]

Picture against white and red background:

Transparent rectangle

share|improve this answer
    
Thank you I was not aware of "Transparent". I Would really like to have the White Rectangle, the first in the Graphics[] Transparent instead, keeping its edge. Is it possible ? I have not been able to apply it to one object only. –  500 Feb 3 '12 at 3:47
    
@500, does the edited version work? –  kguler Feb 3 '12 at 4:36
    
@kguler.Perfectly, Thank you again. –  500 Feb 3 '12 at 4:51
add comment

If you are already dealing with rasterized graphics, ImageCrop[image] tries to remove uniformly colored edges.

In version 8.01 there was a bug that sometimes got it wrong, an equivalent workaround was:

imcrop[img_] := ImagePad[img, -BorderDimensions[img, 0]]

which also shows the rather useful BorderDimensions[] in action.

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.