简体   繁体   中英

How to preserve image metadata in MATLAB imwrite?

I want to preserve image metadata but my code strips all metadata. How do I stop this? I run this script from the MATLAB SDE command prompt. Code is below:

p = which('G0011363.JPG');
filelist = dir([fileparts(p) filesep '*.JPG']);
fileNames = {filelist.name};
fileNames_size = size(fileNames,2);
number_of_colums = fileNames_size;
for k = 1:number_of_colums
   imwrite(undistortImage(imread(fileNames{k}), cameraParams2cof, 'OutputView', 'valid'), (strcat(int2str(k), 'R2_3COF_ONRcorrected.jpg')));
end

You can read in metadata using imfinfo , and you can write specific metadata to your image file with imwrite (as long as the particular tag is supported) as key/value pairs. Look at help imwrite for more info.

Example:

>> I = imread('NeverGonnaGiveYouUp.png');          
>> imwrite(I, 'output.png', 'png','Author','Rick Astley');
>> Iinfo = imfinfo('output.png'); 
>> Iinfo.Author
ans =
Rick Astley

I found a solution, but it isn't perfect...

Note: The posted solution applies Windows OS.


Where referring to image metadata, I assume you mean Exif data .

According to https://www.mathworks.com/matlabcentral/answers/152559-writing-exif-data-to-jpg

Unfortunately, there is currently no off-the-shelf functionality to write EXIF data to an image file in MATLAB. You can only read EXIF data from an image file ( exifread and imfinfo ).

You can use run_exiftool do copy Exif data from one image to another:

  1. Download and extract exiftool-10.25.zip
  2. Copy file exiftool(-k).exe to your working folder, and rename file to exiftool.exe
  3. Download run_exiftool from https://www.mathworks.com/matlabcentral/fileexchange/42000-run-exiftool , Copy getexif.m and putexif.m to your working folder.

Try the following code sample:

%Copy the file from c:\Program Files\MATLAB\R2014b\mcr\toolbox\matlab\demos\ to local folder.
%Note: ngc6543a.jpg is part of Matlab installation.
copyfile([matlabroot, '/mcr/toolbox/matlab/demos/ngc6543a.jpg'], cd);

%Read image
I = imread('ngc6543a.jpg');

%Save I to myfile.jpg and add Exif data of ngc6543a.jpg to myfile.jpg
status = putexif(I, 'myfile.jpg', 'ngc6543a.jpg');

%Read Exif data from ngc6543a.jpg
[ngc6543a_exifdata, ngc6543a_nf] = getexif('ngc6543a.jpg');

%Read Exif data from myfile.jpg
[myfile_exifdata, myfilenf] = getexif('myfile.jpg');

I am getting a warning message: Warning: Exif tags may not have been copied , but it seems to work.

Result:

>> ngc6543a_exifdata

ngc6543a_exifdata =

ExifToolVersion                 : 10.25
FileName                        : ngc6543a.jpg
Directory                       : .
FileSize                        : 27 kB
FileModifyDate                  : 2014:07:27 12:00:28+03:00
FileAccessDate                  : 2016:08:14 17:42:23+03:00
FileCreateDate                  : 2016:08:14 17:18:27+03:00
FilePermissions                 : rw-rw-rw-
FileType                        : JPEG
FileTypeExtension               : jpg
MIMEType                        : image/jpeg
JFIFVersion                     : 1.01
ResolutionUnit                  : None
XResolution                     : 1
YResolution                     : 1
Comment                         : CREATOR: XV Version 3.00b  Rev: 6/15/94  Quality = 75, Smoothing = 0.
ImageWidth                      : 600
ImageHeight                     : 650
EncodingProcess                 : Baseline DCT, Huffman coding
BitsPerSample                   : 8
ColorComponents                 : 3
YCbCrSubSampling                : YCbCr4:2:0 (2 2)
ImageSize                       : 600x650
Megapixels                      : 0.390


>> myfile_exifdata

myfile_exifdata =

ExifToolVersion                 : 10.25
FileName                        : myfile.jpg
Directory                       : .
FileSize                        : 75 kB
FileModifyDate                  : 2016:08:14 18:08:51+03:00
FileAccessDate                  : 2016:08:14 18:08:51+03:00
FileCreateDate                  : 2016:08:14 17:40:22+03:00
FilePermissions                 : rw-rw-rw-
FileType                        : JPEG
FileTypeExtension               : jpg
MIMEType                        : image/jpeg
JFIFVersion                     : 1.01
ResolutionUnit                  : None
XResolution                     : 1
YResolution                     : 1
Comment                         : CREATOR: XV Version 3.00b  Rev: 6/15/94  Quality = 75, Smoothing = 0.
ImageWidth                      : 600
ImageHeight                     : 650
EncodingProcess                 : Baseline DCT, Huffman coding
BitsPerSample                   : 8
ColorComponents                 : 3
YCbCrSubSampling                : YCbCr4:2:0 (2 2)
ImageSize                       : 600x650
Megapixels                      : 0.390

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM