File Exchange

image thumbnail

run_exiftool

version 1.1.0.0 (50.5 KB) by Peter Burns
Read and write full Exif metadata from image files. A Matlab front end for ExifTool, by Phil Harvey.

25 Downloads

Updated 22 Jul 2013

View License

Two functions:
getexif: read Exif matadata
putexif: save image with matedata
This is a front end for the command-line executable, ExifTool. It can also serve as an example of how to call other programs from Matlab. This is useful when a reference implementation is available as a command-line version.

Comments and Ratings (20)

g = getexif('C:\LogFolders\Video_1562715681114.mp4');
exif=struct;
lines=split(g,char(10));
for kk=1:length(lines)-1
parts=split(lines{kk},':');

if length(parts) == 2
exif.(deblank(parts{1}))=deblank(parts{2});
elseif length(parts) > 2
exif.(deblank(parts{1}))=deblank(strjoin(parts(2:length(parts)), ':'));
end
end

Handled the case where the file metadata has the ':' character in it. Works in most case.

PS: Updated using Jon Kocbach's answer as a reference

getexif(fname) works, produces what looks like a character array of property value pairs. nf1 = 41, nf2 = 44 and nf3 = 44 on a .jp2 file originating from Sentinel imagery. However, we're getting the `Warning: Exif tags may not have been copied' and no file with the `_original' suffix is written. I am also somewhat expecting to see corner lat and lon metadata in the file but do not see these.

getexif(fname) works, produces what looks like a character array of property value pairs. nf1 = 41, nf2 = 44 and nf3 = 44 on a .jp2 file originating from Sentinel imagery.

Currently exploring this function... I had to change
imwrite(dat, fname, 'quality', 100);
to
imwrite(dat, fname)
in putexif.m

Can we edit the exif data and write it out, without relying on a 'refname' file? The application is adding the latitude longitude information to each file from an external list of lot/lon pairs i.e. 'geotagging' the images

Jan Kocbach

Thanks for a nice script. It would be nice to get the info out in a struct. You can do that using e.g.:

g = getexif('d:\download\MOV_3564.mp4');
exif=struct;
lines=split(g,char(10));
for kk=1:length(lines)-1
parts=split(lines{kk},':');
exif.(deblank(parts{1}))=deblank(parts{2});
end

Mo

Very helpful!

Perfect! For this to work, it's needed to download the "exiftool(-k).exe" from their website " https://sno.phy.queensu.ca/~phil/exiftool/ " . After downloaded you need to rename "exiftool(-k).exe" to "exiftool.exe" and add it to path.

Gin Chin

missing exiftool.exe

Michelle

I'm not sure if this problem comes up a lot, but this script as-is doesn't work on Macs. Here's how I edited it.

function [exifdata, nf] = getexif(fname)
%[status] = getexif(fname) read Exif data from an image file
% fname = name of image file, e.g., 'myfile.jpg'
%
%Needs ExifTool.exe, written by Phil Harvey
% http://www.sno.phy.queensu.ca/~phil/exiftool/
% 1. Download the file, exiftool(-k).exe
% 2. Rename this file, exiftool.exe
% 3. Save this file in a folder on your Matlab path, e.g. .../matlab/
%Peter Burns, 28 May 2013
% 22 July 2013, following suggestions from jhh and Jonathan via
% Matlab Central.

% Modified by Michelle Maiden to be Mac-patible

if strcmp('PCWIN',computer) %If this is a windows machine
test = which('exiftool.exe');
if isempty(test)
disp('ExifTool not available:');
disp('Please download from,')
disp(' http://www.sno.phy.queensu.ca/~phil/exiftool/')
disp('make sure that the installed exiftool.exe is on your Matlab path')
beep
exifdata=[];
nf = 0;
return
end
elseif strcmp('MACI64',computer)
test = '/usr/local/bin/exiftool';
if isempty(test)
disp('ExifTool not available:');
disp('Please download from,')
disp(' http://www.sno.phy.queensu.ca/~phil/exiftool/')
disp('make sure that the exiftool installer has been run')
beep
exifdata=[];
nf = 0;
return
end
else
disp('Computer type not recognized.');
end
TS=[ '"' test '" -s "' fname '"'];
[status, exifdata] = system(TS);

nf = find(exifdata==':');
nf = length(nf);
end

Is there a Linux version for this script?

I would like to add a new tag to a Image. How can I do that with this code?

I exactly wish to add the GPSInfo tag to a JPEG or TIFF image. I want to add GPS information from other device into the images

works like a charm

Peter Burns

Update is posted now.

Peter Burns

Note: I have submitted an update. It has not been posted yet.

Peter Burns

I have updated the files as suggested by jhh and Jonathan. Thanks.

Also, we can suppress the '1 image files updated', returned by ExifTool to the Command window by adding a second argument, so in putexif.m,
%[status] = system(temp1);
is replaced by,
[status, junk] = system(temp1);
or
[status, ~] = system(temp1);
I have included this in the update.

Jonathan

Thanks jhh, that did the tick,
For others you can get the second file, putexif.m to work by making the following changes to the else section:

%-----
test = which('exiftool.exe');
[exifdata, nf] = getexif(refname);
% Save data as image file with Matlab metadata
imwrite(dat, fname, 'quality', 100);
[exifdata2, nf2] = getexif(fname);
% Replace matadata from this file with desired tags from reference file
temp1=['"' test '" -m -tagsfromfile "',refname,'" -all:all "', fname, '"'];
status = system(temp1);

% Approximate check
[exifdata3, nf3] = getexif(fname);

% Delete extra copy of reference image file
temp3 = ['del "',fname,'_original"'];
status = system(temp3);

% Test approx. number of tags
if abs(nf3-nf2<10)
disp('Warning: Exif tags may not have been copied');
status = 1;
end
%----

I can't figure out how to suppress the output saying that '1 image files was updated' as I think it is being passed from exiftool.exe

jhh

I've got it running now by (1) using 'addpath' to tell Matlab where exiftool can be found and (2) changing the 'else' part of getexif.m as follows:

TS=[ '"' test '" -s "' fname '"']
[status, exifdata]=system(TS);
nf = find(exifdata==':');
nf = length(nf);

But even then, you just get exifdata as a 1xn char array, you you still have to decipher that into meaningfull variables. (I've done so for myself, and it works, but it's not mature enough to post it on FileExchange).

I suppose nf should reurn the number of fields in the exif data, but counting the ':' is not the right way to do it.

Jonathan

I'm having the same issue as jhh, with it not acculay running

jhh

Sounds like just what I need, but is not working on my syetem (R2007b).
I get: "'exiftool.exe' is not recognized as an internal or external command,
operable program or batch file."
Even though exiftool.exe is in my path (confirmed by 'whcih exiftool.exe')

Updates

1.1.0.0

Improved following suggestions from jhh and Jonathan. Thanks.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux