Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Precise bitmaps

8 views
Skip to first unread message

Jan G. Korvink

unread,
Feb 27, 2005, 1:48:36 AM2/27/05
to
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

Steve Luttrell

unread,
Feb 28, 2005, 3:39:06 AM2/28/05
to
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
place.

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"]
Show[g2]

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.

Dimensions[g2[[1,1]]]
g2[[1,2]]
g2[[1,3]]
g2[[1,4]]
g2[[2]]
g2[[3]]
g2[[4]]

{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 -
1}};
Show[g3]

Have a look at the contents of g3.

Dimensions[g3[[1,1]]]
g3[[1,2]]
g3[[1,3]]
g3[[1,4]]
g3[[2]]
g3[[3]]
g3[[4]]

{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" <kor...@t-online.de> wrote in message
news:cvrqg4$p55$1...@smc.vnet.net...

Steve Luttrell

unread,
Feb 28, 2005, 3:45:56 AM2/28/05
to
Further to my previous reply.

1. Mathematica 5.1 has NOT cured the problem.
2. The cropping operation to remove the extra white space is
Drop[Map[Drop[#,-1]&,g2[[1,1]]],1]

Steve Luttrell


DrBob

unread,
Mar 1, 2005, 2:13:19 AM3/1/05
to
In version 5 and above, that's the same as:

Rest[Most /@ g2[[1, 1]]]

Bobby

--
Dr...@bigfoot.com
www.eclecticdreams.net

Stefano Taschini

unread,
Mar 4, 2005, 5:14:34 AM3/4/05
to
Jan,

The quality of the raster engine in Mathematica is anyway rather poor,
and I'd rather use an external tool.

The whole procedure can be automatized. If you have ImageMagick and
GhostScript installed on your Mac, you can use the following function

Options[Rasterize] = {ImageSize -> 288};
Rasterize[g_, opts___?OptionQ] := With[{
f = OpenTemporary[],
fullopts = Join[{opts}, Options@Rasterize]},
Close[f];
Export[First@f, g, "EPS"];
Run@StringForm["sh -l -c 'convert -resize `2` eps:`1` bmp:`1`'",
First@f,
Replace[Replace[ImageSize, fullopts], {
n_Integer :> StringForm["`1`x`1`", n],
{x_, y_} :> StringForm["``x``!", x, y]}]];
With[{bmp = Import[First@f, "BMP"]},
DeleteFile[First@f];
bmp]]

For instance, you can rasterize the thorus of a previous reply,

<< Graphics`Shapes`
g=Graphics3D[Torus[],Background->RGBColor[0,0,0]];
Show[Rasterize[g]]

With the option ImageSize you can set max(width,height)

Show[Rasterize[g, ImageSize -> 100]]

You can also set exactly both width and height

Show[Rasterize[g, ImageSize->{100,200}]]

I tested the Rasterize function with Mathematica 5.1 on Mac Os X 10.3.8,
but should work on any unix system, provided you have ImageMagick and
GhostScript installed.

Ciao,
Stefano

0 new messages