Tiff.setTag

Sets the value of a TIFF tag before writing an image.

Category: Data Import and Analysis > Data Import and Export

Syntax

setTag(t, tagName, value)

Sets one tag.

setTag(t, tagStruct)

Sets several tags at once from a struct whose fields are tag names.

Description

The method acts on a Tiff object created with Tiff(filename, mode). Before write, a new image needs at least its width and length, bits and samples per pixel, photometric interpretation and either rows per strip or tile size. After writeDirectory, the tags must be set again for the next image.

Input arguments

t
Tiff object opened for writing.
tagName
Tag name such as 'ImageWidth', 'ImageLength', 'BitsPerSample', 'SamplesPerPixel', 'Photometric', 'RowsPerStrip', 'PlanarConfiguration' or 'Compression'.
value
Tag value. For coded tags use the constants of the class, such as Tiff.Photometric.MinIsBlack or Tiff.Compression.LZW.

Example

Write a compressed image

tag.ImageLength = 8; tag.ImageWidth = 8;
tag.Photometric = Tiff.Photometric.MinIsBlack;
tag.BitsPerSample = 8; tag.SamplesPerPixel = 1; tag.RowsPerStrip = 8;
tag.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tag.Compression = Tiff.Compression.LZW;
w = Tiff('lzw.tif', 'w');
w.setTag(tag);
w.write(uint8(magic(8) * 3));
w.close();
r = Tiff('lzw.tif', 'r');
r.getTag('Compression') == Tiff.Compression.LZW
r.read()
r.close();
Output
ans = logical
   1
ans = 8×8 uint8 matrix

   192     6     9   183   180    18    21   171
    27   165   162    36    39   153   150    48
    51   141   138    60    63   129   126    72
   120    78    81   111   108    90    93    99
    96   102   105    87    84   114   117    75
   123    69    66   132   135    57    54   144
   147    45    42   156   159    33    30   168
    24   174   177    15    12   186   189     3

See also

Tiff.getTag Tiff.getTagNames Tiff.rewriteDirectory imwrite