MathGroup Archive 2007

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

Search the Archive

Re: Exporting images without white borders


On Apr 23, 11:36 am, "Steven T. Hatton" <hatt... at globalsymmetry.com>
wrote:
> If I import an image into a notebook and use the raster to create an image
> with the same number of pixels, when I export the image it always has a
> white frame around it.

This is very disappointing, but it seems that there is no simple way
of exporting a matrix as a pixel-perfect image from Mathematica (or at
least I couldn't find any in the documentation). When using Export
with a graphics format, the data is always converted to a vector image
first, so small distortions may be introduced during rasterization.
(And I wouldn't be surprised if it turned out that the end result is a
little bit different for different platforms. I wouldn't trust a
solution that works by tweaking PlotRange and ImageSize values.)

A workaround is to write Mathematica code to export the data to some
very simple image format, e.g. a portable graymap. You can find the
description of the format here:

http://en.wikipedia.org/wiki/Portable_pixmap
http://netpbm.sourceforge.net/doc/pgm.html

img = Import["some grayscale image"]
dat = img[[1,1]]

Export[
    "img.pgm",
    {{"P2"}, (* this specifies the image format *)
     Reverse@Dimensions@dat, (* the image dimensions *)
     {Max@dat} (* the value corresponding to white *)
    } ~Join~ Reverse@dat,
    "Table"];

You may have to figure out the white value yourself: it may be
represented in Raster[] in different ways depending on the format of
the image you imported.

There are many free programs that can read pgm files and convert them
to other formats:
http://www.irfanview.com/
http://www.imagemagick.org/

I hope this helps.

Szabolcs



  • Prev by Date: Re: CrossProduct in Spherical Coordinates
  • Next by Date: Re: What's wrong?
  • Previous by thread: Re: Exporting images without white borders
  • Next by thread: short cut