-->

How do I append metadata to an image in Matlab?

2020-08-26 03:56发布

问题:

In writing some image processing routines in Matlab, I found that I don't know how to write metadata in to a newly processed and saved image. To keep it simple, my flow is as follows:

image = imread('Base_Pic.jpg');  
image_info = imfinfo('Base_Pic.jpg');
%Process image...
%Update metadata...
imwrite(image,'Updated_Image.jpg','JPEG','Quality',100);

I basically want the newly processed image to have all the same metadata attributes as the original image, with a few fields updated of course.

How can I append the image_info structure to the newly saved JPEG?

回答1:

You have a (very) limited ability to do this in imwrite : for JPEG it only accepts BitDepth, Comment, Mode and Quality. And Mode and Quality don't get returned from iminfo. In imwrite you can do:

iminfo = imfinfo('Base_Pic.jpg')
imwrite(...,'BitDepth',iminfo.BitDepth, 'Comment',iminfo.Comment);

Other than that, there isn't a way to do this with Image Processing Toolbox/Matlab as far as I know. If you have TIFF or medical images there are a number of toolboxes that do it, but I haven't ever found any for jpeg, even on the File Exchange...

If you have exiftool on your system, you can use

[status info]=system('exiftool -s Base_Pic.jpg');

info now contains a list of tag names and tag values, e.g.:

ExifToolVersion                 : 8.75
FileName                        : Base_Pic.jpg
Directory                       : Pictures
FileSize                        : 2.0 MB
FileModifyDate                  : 2011:10:27 08:41:55+10:00
FilePermissions                 : rw-rw-r--
FileType                        : JPEG
MIMEType                        : image/jpeg
JFIFVersion                     : 1.01
ExifByteOrder                   : Big-endian (Motorola, MM)
Make                            : Apple
Model                           : iPhone 4
...

And if you split on colon : you can write them back using exiftool -[TAG]=[VALUE], e.g. exiftool -Make=Apple -Model="iPhone 4" ....

Or you can copy them "all" in one foul hit:

system('exiftool -overwrite_original -tagsFromFile Base_Pic.jpg Updated_Image.jpg')


回答2:

Read this : http://www.mathworks.co.uk/help/matlab/import_export/exporting-to-images.html#br_c_iz-1

under the title : "Exporting Image Data and Metadata to TIFF Files" ...



回答3:

If you aren't modifying too many of the fields you could make use of exiftool, which is an executable which can read and write exif tags.

I'd probably do something like:

image = imread('src.jpg');  
image_info = imfinfo('src.jpg');
%Process image...
%Update metadata...
imwrite(image,'dst.jpg','JPEG','Quality',100);

% copy over all the tags
system('exiftool -tagsfromfile src.jpg dst.jpg');

% then use exif tool to update the specific tags
...