Transferring tiff image tags identically to another image

13 views (last 30 days)
I have a script that reads tif images, manipulates them via image processing toolbox and saves it back to disk. The tif files have user defined data in the tags. I would like to pass these onto the new images saved. How can I do that? I started reading methods for reading tags etc but it seems like it's too much work just to copy things over. Can anyone suggest a simpler method?

Accepted Answer

Walter Roberson
Walter Roberson on 27 May 2015
The easiest way for that flow:
1) make a copy of the .tif file using copyfile or equivalent
2) open the copy using the tiff class and the 'r+' option:
t = Tiff('ImageCopy.tif', 'r+');
3) ImgData = read(t);
4) Process ImgData, producing NewImgData
5) Providing that the new image array is *exactly* the same size as the old one, write() the image
write(t, NewImgData);
6) close(t);
If the new image is a different size, that can be handled relatively easily.
The above flow presumes that you want to modify the first image in the TIFF file, and that it does not have any sub-images
If you need to modify an image other than the first in the file, or if you need to deal with sub-images, then matters become more complex.
  3 Comments
RT
RT on 19 Sep 2017
Hi Walter, since NewImgData is a matlab variable, how do you export it back to the disk while preserving the tags that was just copied from ImageCopy.tif? Thanks, Rei
Walter Roberson
Walter Roberson on 19 Sep 2017
Looking back over my code, the expectation is that when you write() the image over top of the existing image that it leaves the tag information in place. The Tiff class allows you to talk about writing image data separately from writing the other associated information.
At the moment, I do not recall having tested the above code, so I am not certain how I arrived at it.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!