Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Precise bitmaps

  • To: mathgroup at
  • Subject: [mg54840] Re: Precise bitmaps
  • From: Stefano Taschini <taschini at>
  • Date: Fri, 4 Mar 2005 05:07:38 -0500 (EST)
  • References: <cvrqg4$p55$>
  • Sender: owner-wri-mathgroup at


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]},
     Export[First@f, g, "EPS"];
     Run@StringForm["sh -l -c 'convert -resize `2` eps:`1` bmp:`1`'",
         Replace[Replace[ImageSize, fullopts], {
           n_Integer :> StringForm["`1`x`1`", n],
           {x_, y_} :> StringForm["``x``!", x, y]}]];
     With[{bmp = Import[First@f, "BMP"]},

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

<< Graphics`Shapes`

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.


Jan G. Korvink wrote:

> 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: Bug in Import?
  • Next by Date: Re: Blurring with mathematica
  • Previous by thread: Re: Re: Re: Why does mathematica randomly rewrite notebooks?
  • Next by thread: Front End Programming question