tiff file tags not saved

조회 수: 6 (최근 30일)
steve solomon
steve solomon 2025년 6월 24일
답변: steve solomon 2025년 7월 15일
I am able to create and save tiff files but the tags I create don't appear to be saved when i view the "properties" from Windows file explorer nor am I able to open the same file with
file = Tiff(tiffFileName,"r");
Curiously, the data is there - when I drag the file from the file explorer to the command line and let the import fcn do its thing, the image data there.
Here's the code I'm using to write the file:
t = Tiff([dataDir filesep saveFileName],"w");
setTag(t,"ImageLength",1024);
setTag(t,"ImageWidth",1024);
setTag(t,"Photometric",Tiff.Photometric.MinIsBlack);
setTag(t,"Compression",Tiff.Compression.None);
setTag(t,"BitsPerSample", 64);
setTag(t,"SampleFormat", Tiff.SampleFormat.IEEEFP);
setTag(t,"SamplesPerPixel", 1);
setTag(t,'ImageDescription', myMetaData);
write(t, data)

채택된 답변

steve solomon
steve solomon 2025년 7월 15일
Yup, it was the ordering of the tiff tags. When i write the tags per the order per the TIFF specs found in the Library of Congress website, all the tags appeared.
Happy trails and thanks to all for the help & feedback.

추가 답변 (1개)

DGM
DGM 2025년 6월 24일
편집: DGM 2025년 6월 25일
I think the main problem here is that you're not closing the file. There are also some annoyances you'll run into if you're trying to set tags this way using setTag(). If you do it this way, you need to set the tags in a certain order in order for them to work right. Certain tags need to be set before others can be set. Alternatively, you can put all the tags in a struct in any order is convenient, and then you can write that tag struct to the Tiff object at once. That's how I'd do it.
This is mostly copy-pasted from another answer I posted. It's more than what you need, but it's generalized enough to cover most cases while being flexible. FWIW, the Deflate compression is lossless. Given that we're making giant DPFP TIFF files that probably nothing else can read, there's little risk in complicating the affair, but fair gains to be had over using uncompressed data. Like anything, it depends on the image content.
% a DPFP test image
inpict = imread('cameraman.tif');
inpict = im2double(inpict);
% metadata
myMetaData = 'banana';
% output file name
filename = 'test.tiff';
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% image can be I/RGB, 8/16/32b uint/int or 32/64b float
% get to work
sz = size(inpict,1:3);
inclass = class(inpict);
t = Tiff(filename, 'w');
tagstruct.ImageLength = sz(1);
tagstruct.ImageWidth = sz(2);
tagstruct.Compression = Tiff.Compression.AdobeDeflate;
tagstruct.SamplesPerPixel = sz(3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
% I/RGB?
% i'm choosing to presume that 3ch inputs are RGB
if sz(3) == 1
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
elseif sz(3) == 3
tagstruct.Photometric = Tiff.Photometric.RGB;
end
% what is the numeric type?
if isinteger(inpict) && inclass(1) == 'u'
tagstruct.SampleFormat = Tiff.SampleFormat.UInt;
elseif isinteger(inpict) && inclass(1) == 'i'
tagstruct.SampleFormat = Tiff.SampleFormat.Int;
elseif isfloat(inpict)
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
else
error('this demo only supports integer and float inputs')
end
% what's the sample width?
switch inclass
case {'uint8','int8'}
tagstruct.BitsPerSample = 8;
case {'uint16','int16'}
tagstruct.BitsPerSample = 16;
case {'uint32','int32','single'}
tagstruct.BitsPerSample = 32;
case {'uint64','int64'}
error('LibTIFF does not support 64b outputs except for double-precision floats')
case 'double'
tagstruct.BitsPerSample = 64;
otherwise
error('this demo only supports integer and float inputs')
end
% additional tags
tagstruct.ImageDescription = myMetaData;
% write everything and close the file
t.setTag(tagstruct);
t.write(inpict);
t.close();
% read the image back
newpict = imread(filename);
isequal(inpict,newpict) % test that it's the same
ans = logical
1
% check the tags
imfinfo(filename)
ans = struct with fields:
Filename: '/users/mss.system.wrwxh/test.tiff' FileModDate: '24-Jun-2025 22:24:40' FileSize: 73863 Format: 'tif' FormatVersion: [] Width: 256 Height: 256 BitDepth: 64 ColorType: 'grayscale' FormatSignature: [73 73 42 0] ByteOrder: 'little-endian' NewSubFileType: 0 BitsPerSample: 64 Compression: 'Deflate' PhotometricInterpretation: 'BlackIsZero' StripOffsets: 8 SamplesPerPixel: 1 RowsPerStrip: 256 StripByteCounts: 73697 XResolution: [] YResolution: [] ResolutionUnit: 'Inch' Colormap: [] PlanarConfiguration: 'Chunky' TileWidth: [] TileLength: [] TileOffsets: [] TileByteCounts: [] Orientation: 1 AutoOrientedWidth: 256 AutoOrientedHeight: 256 FillOrder: 1 GrayResponseUnit: 0.0100 MaxSampleValue: 1.8447e+19 MinSampleValue: 0 Thresholding: 1 Offset: 73706 ImageDescription: 'banana' SampleFormat: 'IEEE floating point'
EDIT:
It might not be relevant here, but I forgot to mention that signed-integer outputs cannot be RGB. It's a niche concern, but this demo code doesn't check for that. I also made no accomodation for logical images. Take that FWIW.
  댓글 수: 4
steve solomon
steve solomon 2025년 6월 25일
편집: Walter Roberson 2025년 6월 25일
Thanks guys, appreciate the quick feedback. I am closing the file, just didn’t include that command in the code snippet. Tried writing the tags to a struct per DGM’s suggestion and that solved one problem, namely it let me open the files from inside Matlab but I still don’t see any of the tags from Windows file explorer.
Went looking for documentation re the ordering of “tag writes” and found tihs on the library of congress website:
The TIFF specification requires that tags be encoded in numerical order and that is the sequence used in the table below.
Will give that a shot and report back.
DGM
DGM 2025년 6월 25일
I have no idea how Windows treats anything, but at least with exiftool, the tags all show up.
>>$ exiftool test.tiff
ExifTool Version Number : 9.46
File Name : test.tiff
Directory : .
File Size : 72 kB
File Modification Date/Time : 2025:06:24 17:23:17-05:00
File Access Date/Time : 2025:06:24 17:23:21-05:00
File Inode Change Date/Time : 2025:06:24 17:23:17-05:00
File Permissions : rw-r--r--
File Type : TIFF
MIME Type : image/tiff
Exif Byte Order : Little-endian (Intel, II)
Image Width : 256
Image Height : 256
Bits Per Sample : 64
Compression : Adobe Deflate
Photometric Interpretation : BlackIsZero
Image Description : banana
Strip Offsets : 8
Samples Per Pixel : 1
Rows Per Strip : 256
Strip Byte Counts : 73697
Planar Configuration : Chunky
Sample Format : Float
Image Size : 256x256
Other tools (e.g. gthumb) may not decode the image data, but the tags are all present.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by