我找到了解決辦法,但它並非十全十美......
注:發佈解決方案適用於Windows操作系統。
凡涉及圖像元數據,我假設你的意思是Exif data。
根據https://www.mathworks.com/matlabcentral/answers/152559-writing-exif-data-to-jpg
不幸的是,目前還沒有現成的現成功能,EXIF數據寫入到MATLAB的圖像文件。您只能讀取圖像文件(exifread和imfinfo)中的EXIF數據。
您可以使用run_exiftool也從一個圖像複製的Exif數據到另一個:
- 下載並解壓縮exiftool-10.25.zip
- 複製文件exiftool(-k).exe文件到您的工作文件夾,重命名文件從https://www.mathworks.com/matlabcentral/fileexchange/42000-run-exiftool exiftool.exe
- 下載run_exiftool,複製getexif.m和putexif.m到工作文件夾。
嘗試下面的代碼示例:
%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');
我得到一個警告消息:Warning: Exif tags may not have been copied
,但它似乎工作。
結果:
>> 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
'imread'不會在元數據讀取只是像素數據。那麼如果從未讀入元數據,你會如何期望'imwrite'寫入元數據? – Suever