17
$\begingroup$

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] 
$\endgroup$
1
  • 2
    $\begingroup$ With png files, you can use Export with option Background->None; but with jpg images this trick does not work. $\endgroup$ Commented Feb 3, 2012 at 3:37

4 Answers 4

26
$\begingroup$

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.

$\endgroup$
5
  • 2
    $\begingroup$ +1, I didn't know "ShrinkWrap" worked in 2D graphics. $\endgroup$ Commented Feb 3, 2012 at 3:37
  • $\begingroup$ @Oleksandr, definetly does it, Thank You :-) $\endgroup$ Commented Feb 3, 2012 at 3:42
  • $\begingroup$ 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. $\endgroup$ Commented Feb 3, 2012 at 3:50
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Feb 3, 2012 at 3:56
  • 3
    $\begingroup$ "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. $\endgroup$ Commented Feb 7, 2012 at 8:19
9
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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 ? $\endgroup$ Commented Feb 3, 2012 at 3:22
9
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Feb 3, 2012 at 3:47
  • $\begingroup$ @500, does the edited version work? $\endgroup$ Commented Feb 3, 2012 at 4:36
  • $\begingroup$ @kguler.Perfectly, Thank you again. $\endgroup$ Commented Feb 3, 2012 at 4:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.