1
$\begingroup$

The following should create a 50x50 image named /tmp/nopad.gif with mostly random colors and a red border:

 
t1024 = Table[If[i==1 || i==50 || j==1 || j==50, {1,0,0}, RandomReal[1,3]],  
 {i, 1, 50}, {j, 1, 50}]; 
t1025 = Graphics[Raster[t1024], PlotRangePadding -> 0]; 
Export["/tmp/nopad.gif", t1025, ImageSize -> {50,50}] 

Here's the image: enter image description here

It's hard to tell directly, but if you load the image into ImageMagick's display and use the magnify feature, you'll see a one pixel white border at the right and bottom edges:

enter image description here

Note that display counts starting at 0, so the highlighted white pixel is the 50th pixel in both the x and y directions.

As nearly as I can tell, setting ImageSize -> {51,51} and then trimming the result is a workaround, but is there a better way to get rid of this border?

As a note, I realize I could've just loaded the image back into Mathematica to confirm it has a border, but, hopefully, the above is convincing enough.

$\endgroup$

1 Answer 1

1
$\begingroup$

Try adding ImagePadding -> None:

t1024 = Table[
   If[i == 1 || i == 50 || j == 1 || j == 50, {1, 0, 0}, 
    RandomReal[1, 3]], {i, 1, 50}, {j, 1, 50}];
t1025 = Graphics[Raster[t1024], PlotRangePadding -> 0, ImagePadding -> None];
Export["/tmp/nopad.gif", t1025, ImageSize -> {50, 50}]

CellPrint@
 ExpressionCell[Import["/tmp/nopad.gif"], Background -> Black, Magnification -> 3]

enter image description here

$\endgroup$
1
  • $\begingroup$ That did the trick, thanks! $\endgroup$
    – user1722
    Commented Aug 23, 2018 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.