Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I feel certain there is a simple answere here.. how do I export image data to a tiff without any scaling?

I want to read back in the exact data.

g = Show[Graphics[{Black, Disk[#, 1]}] & /@ Table[RandomReal[{-10, 10}, 2], {50}]];
datain = ImageData[Rasterize[Style[g, Antialiasing -> False], "ColorSpace"-> "Grayscale"]];

as a more simple example just use:

datain = Table[0, {300}, {400}];

Export["test.tiff", Raster[datain], "BitDepth" -> 1, "ColorSpace" -> "Grayscale" ,
       ImageSize -> Reverse[Dimensions[datain]]];
dataout = Reverse[ImageData[Import["test.tiff"]]];
datain == dataout    ->> False
   Graphics[Raster[(1 + datain - dataout)/2]]}}]

enter image description here

What you see is the image has been shrunk to accomodate an white margin of about 10 pixels. (Most annoying you wouldn't notice this if you didn't look carefull..)

example 2..

enter image description here


Bill S has the solution.. Either Image[datain] or Image[Raster[datain]] or this mess:

      PlotRange -> {{0, Dimensions[datain][[2]]}, {0,Dimensions[datain][[1]]}}]

does the job. The last form I guess gives a clue, presumably Export calls Graphics which is padding by default. None of the documented options ImagePadding, ImageMargins, etc turn this off though.

Edit 2.. need BOTH of these options...go figure

Graphics[Raster[datain], ImagePadding -> None,  PlotRangePadding -> None]
share|improve this question
PlotRangePadding might be the one... – cormullion Apr 26 '13 at 19:29
Rescale might also be useful. – amr Apr 28 '13 at 4:30
up vote 3 down vote accepted

One way to do this is to define your data as an image

datain=Image[Table[0, {300}, {400}];]

and then save the image as a tif

Export["test.tif", datain]

Now when you read it back in:

q = Import["test.tif"]

you can see that they are the same since

ImageData[q] == ImageData[datain]

Of course, this only works under certain conditions: the data has to be representable within the bit depth of the image -- this is inherent to the .tif format. Moreover, you will need to rescale the image so that all values lie between 0 and 1 in order to avoid truncation when saving to a .tif.

Here's an example. Create your data:

w = Image[RandomInteger[{0, 255}, {5, 5}]/255]

Note I've scaled everything between zero and one and the data will fit within a 8-bit depth.

Export["test.tif", w]
q = Import["test.tif"]

The data is intact, since

ImageData[q] == ImageData[w]

returns True.

share|improve this answer

I can't reply to your question directly (too little reputation?) so I have to do it this way. Perhaps this question is related to yours and can help you solving it.

share|improve this answer
thanks, you know I somehow missed that page searching.. – george2079 Apr 26 '13 at 19:36
For what it's worth you now have plenty of "reputation" to comment everywhere. – Mr.Wizard Apr 28 '13 at 10:12
Nice. Thanks everybody for this ;) – chromate Apr 28 '13 at 15:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.