MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Precise bitmaps

I have used precisely the same export/import trick as you in order to use 
the Mathematica rendering engine to generate bitmaps for use in image 
processing applications. My cure to the extra white space problem was to 
simply discard those pixels after Importing the bitmap, and to anticipate 
this loss of pixels by Exporting a slightly larger bitmap in the first 

I have just tried to reproduce what I did using Mathematica 5.1 (for 
Windows) and find that the extra whitespace problem seems to have been cured 
in that version. However, here is (roughly) what I did to cure the problem 
in an earlier version of Mathematica:

Load a package for generating a pretty image.

<< "Graphics`Shapes`"

Generate an image (with a black background so you can see the extra white 
space when it is added), export, then import it. Notice that I export 1 
pixel more in each dimension than I eventually need.

g = Show[Graphics3D[Torus[], Background -> RGBColor[0, 0, 0]]]
Export["temp.bmp", g, "BMP", ImageSize -> {101, 101}]
g2 = Import["temp.bmp", "BMP"]

Now have a look at the contents of g2. The first part is the image data, so 
I have displayed only its dimensions. This shows which bits have to be 
changed if you crop the image. There may be an "official" way of doing this, 
but I haven't found it.


{101, 101, 3}
{{0, 0}, {101, 101}}
{0, 255}
ColorFunction -> RGBColor
ImageSize -> {101, 101}
PlotRange -> {{0, 100}, {1, 101}}
AspectRatio -> Automatic

Make a copy of g2, and then hack it to crop the image. I am dropping the 
last pixel in each row, and also the last row of the image. I am doing this 
from memory, so if this prescription doesn't get rid of the white space then 
modify it appropriately.

g3 = g2;
g3[[1,1]] = Drop[Map[Drop[#,-1]&,g2[[1,1]]],-1];
g3[[1,2,2]] = g3[[1,2,2]] - {1, 1};
g3[[2]] = g3[[2]] /. {x_, y_} -> {x - 1, y - 1};
g3[[3]] = g3[[3]] /. {{x1_, y1_}, {x2_, y2_}} -> {{x1, y1 - 1}, {x2, y2 - 

Have a look at the contents of g3.


{100, 100, 3}
{{0, 0}, {100, 100}}
{0, 255}
ColorFunction -> RGBColor
ImageSize -> {100, 100}
PlotRange -> {{0, 99}, {1, 100}}
AspectRatio -> Automatic

That should do what you want.

Steve Luttrell

"Jan G. Korvink" <korvink at> wrote in message 
news:cvrqg4$p55$1 at
> Dear All,
> I am battling to obtain a "precise" Raster[] of some Graphics[] objects.
> There seems to be no straightforward way to do the conversion, so I tried:
> Export[ /tmp/myfile, Graphics[myGraphics], ImageSize -> {xmax,ymax}
> myBitsPerUnit ]
> g = Import[ /tmp/myfile ]
> from which I can unpack the Raster[]. Unfortunately the Raster[]
> contains extra white space around the object, and no matter what I
> select for "myfile" (I have tried all bitmap formats) I get a
> consistently bad result. Furthermore, the Export/Import route is pretty
> bad, since it is slow for large pictures.
> What works is to use Adobe Illustrator as the rendering engine. But this
> is not nice!
> I do not want to implement a renderer, unless absolutely necessary, and
> it seems silly since Mathematica has such an animal.
> Can anyone suggest an alternative route?
> Thanks in advance, Jan

  • Prev by Date: Re: Re: Bounds for Trig expression
  • Previous by thread: Precise bitmaps
  • Next by thread: Construcing correlation matrix from time-ordered list