16

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]
500
  • 5,569
  • 6
  • 27
  • 49
  • 2
    With png files, you can use Export with option Background->None; but with jpg images this trick does not work. – kglr Feb 03 '12 at 03:37

4 Answers4

25

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.

Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125
  • 2
    +1, I didn't know "ShrinkWrap" worked in 2D graphics. – Brett Champion Feb 03 '12 at 03:37
  • @Oleksandr, definetly does it, Thank You :-) – 500 Feb 03 '12 at 03: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 03 '12 at 03: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 03 '12 at 03: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 07 '12 at 08:19
9

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Yves Klett
  • 15,383
  • 5
  • 57
  • 124
8

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.

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
  • 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 03 '12 at 03:22
5

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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 03 '12 at 03:47
  • @500, does the edited version work? – kglr Feb 03 '12 at 04:36
  • @kguler.Perfectly, Thank you again. – 500 Feb 03 '12 at 04:51