我找到了解决办法,但它并非十全十美......
注:发布解决方案适用于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