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]
  • 2
    $\begingroup$ PlotRangePadding might be the one... $\endgroup$ – cormullion Apr 26 '13 at 19:29
  • $\begingroup$ Rescale might also be useful. $\endgroup$ – amr Apr 28 '13 at 4:30

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.


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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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