File Exchange

image thumbnail

Export image to TIF or TIFF file of selected data type

version (138 KB) by Zhang Jiang
A supplement to IMWRITE to write to TIF files of data types beyond int16.


Updated 24 Feb 2011

View License

IMWRITE2TIF Write image to tif file with specified datatype.

IMWRITE2TIF(IMGDATA,HEADER,IMFILE,DATATYPE)exports IMGDATA with HEADER to TIF file named IMFILE. HEADER is usally obtained by IMFINFO from original image file, and it can also be left empty. String DATATYPE specifies data type for the export. Supported data types include logical, uint8, int8, uint16, int16, uint32, int32, uint64, int64, single and double.

IMWRITE2TIF(IMGDATA,HEADER,IMFILE,DATATYPE,TAG NAME1,TAG VALUE1,TAG NAME2,TAG VALUE2, ...) writes with specified Matlab supported TIF tag values. These new tag values override those already defined in HEADER.

Note 1:
to avoid errors such as '??? Error using ==> tifflib The value for MaxSampleValue must be ...', overide tag MaxSampleValue by Matlab supported values. Or simply remove the tag from HEADER.

Note 2:
Overwriting of the existing image files is not checked. Be cautious with the export image file name.

Example 1:
imgdata = imread('ngc6543a.jpg');
header = imfinfo('ngc6543a.jpg');

Example 2:
imgdata = imread('mri.tif');

More information can be found by searching for 'Exporting Image Data and Metadata to TIFF Files' in Matlab Help.

Cite As

Zhang Jiang (2020). Export image to TIF or TIFF file of selected data type (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

Yee Zuo Koh

thank you it works great

zhang yuantao

samvedya surampudi

samvedya surampudi

Hi Zhang,
Firstly I appreciate your awesome work.
But I am having the following error while running this function.

Error using tifflib
Photoshop must be a uint8 array.

Error in Tiff/setTag (line 1373)

Error in imwrite2tif (line 153)

When I tried to look into tifflib it is showing"There is no documentation for tifflib"
I wanted to write a uint8 tiff file to double precision Tiff by using imwrite2tiff


Please let me know where I am doing wrong.

Jaladhar Mahato

Is it possible to set colormap in the output image?

Zhouping wei

When I used IMWRITE2TIF function, I ran into following error messages:

Error using tifflib
RowsPerStrip should be a double precision scalar value.
Error in Tiff/setTag (line 1362)
tifflib('setField',obj.FileID, ...

Error in imwrite2tif (line 153)

Please let me know what's the problem. Thanks!

Laurence Lurio


Hi Zhang,
I was wondering about the compression which you indicate in example2. It doesn't seem to work, my file size stays the same with compression either on or off. Using Photoshop to open and then save a tif with ZIP compression results in only 25% the file size.

It would be very useful to me if that worked. Am I using it wrong (like in the example2)?


extremely useful, thank you. I just don't get why on earth mathworks doesn't support an easy way to write floating point TIFs... really annoying!

John Wu

Mu He



It runs well on relatively newer versions. But when I ran this on a very old version (MATLAB R2006a), it gave me following error:

??? Undefined command/function 'Tiff'.

Error in ==> imwrite2tif at 109
t = Tiff(imfile,'w');

I don't know whether this is fixable. Or I just have to use newer version of MATLAB.



Hi Jiang,
This is great! I too wanted to write a number of images to one file as did Hongxiao. I found this to work:

tagstruct.SamplesPerPixel = 1;%size(imgdata,3);

Added at the end:
nimgs = size(imgdata,3);
if nimgs>1
t2 = Tiff(imfile,'a');

for n=2:nimgs
t2.write(squeeze(imgdata(:,:,nimgs)), 'a');
['writing image' num2str(n)]

Michael W

This function is really great! It works most of the time, but it creates a segmentation violation when writing very large (>2GB) TIFF files. Anyone know about this?

Adam A

I used this to export a floating-point TIFF, and it worked very well. Thanks for making it available.

Matlab should have this as a built-in function.

Hongxiao Jin

Hi, Jiang
This is just wonderful!
This is really what I am looking for. I want to save a tiff file with original double format and hundreds of images.
Just in one file!

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!