Read and Copy Exif data to multiple images Matlab

Started by christinak, February 19, 2017, 09:19:15 AM

Previous topic - Next topic

christinak

Hello,

First of all I am sorry if this is answered somewhere else but I couldn't find a solution.
I am quite new to programming and also to Matlab so if this can be done in Python for example, feel free to give me a push (however I am not sure on how to run exiftool from python).

My problem:
When pre-precessing images in Matlab and using imwrite to save the output, all the metadata from the original images are lost.
I used exiftool from command prompt and saw that it can successfully copy and paste the metadata from one image to another but I need to run it in like for like 1000 of images so its not possible to specify the names and paths everytime.
So I need to create a small loop with exiftool that can 1. read per input image all the metadata and 2.search for the same name image on the output images and 3.copy the medatada.
So far I have something like that

Images = dir('F:\*.jpg'); %Initial images
CorImages = dir('F:\Cor\*jpg'); %Corrected images after pre-processing

for i=1:length (Images)
    im = imread( fullfile('F:\', Images(i).name ) );
    exifdata = getexif(im);
    for k=1:length (CorImages)
        cim = imread( fullfile('F:\Cor', CorImages(i).name) );
        if Images(i).name == CorImages(k).name
            status = putexif(cim); %apparently something missing here?
        end
    end
end

When I run this I get this error
Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Error in getexif (line 26)
TS=[ '"' test '" -s "' fname '"'];

I would appreciate any help..

Hayo Baan

No need for loops :) To copy metadata from existing images in different directories can all be done with a single exiftool command:
exiftool -tagsfromfile F:\%f.%e -ext JPG F:\Cor
This will copy the metadata from the files in F:\ to the files in F:\Cor for all files in F:\Cor.
Or isn't this what you wanted?
Hayo Baan – Photography
Web: www.hayobaan.nl

christinak

#2
Thank you Hayao Baan for your reply..

Yes it works finally also with this command! Why I couldn't find it earlier?
The only thing I notice is that it creates also some other files with the extention _original and in the GPS details it gives only 1 decimal for Altitude information (for example Altitude 85.1 instead of 85.113 which is important for my case), but I am not sure why is that..

But I finally made it work with the loop in Matlab, although indeed it seems there should be an easier way..and faster perhaps because I think it will slow down when it has to iterate through hundreds of images.. I notice in this case with the loop in Matlab the Altitude shows as exactly as the same with all decimals 85.113..
My point was to copy the specific metadata from each image (for example from image F:\0121.jpg to F:\cor\0121.jpg) because I need to retain the GPS location from the original images (which is different in each image that's why I used the if statement... )

For the sake of I made it work like that in Matlab...
for i=1:length(Images)
    im = imread( fullfile('F:\', Images(i).name ) );
    for k=i
    cim = imread( fullfile('F:\Cor', CorImages(k).name) );
        if Images(i).name == CorImages(k).name
            source = strcat('F:\',Images(i).name)
            target = strcat('F:\Cor\',CorImages(k).name)
            exifdata = getexif(source);
            status = putexif(cim,target, source);
        end
    end
end

If there is a faster way it would be so nice, please let me know what you think!

StarGeek

Quote from: christinak on February 19, 2017, 03:30:09 PM
The only thing I notice is that it creates also some other files with the extention _original

Add -overwrite_original to the command to avoid making backups.

Quote
and in the GPS details it gives only 1 decimal for Altitude information (for example Altitude 85.1 instead of 85.113 which is important for my case), but I am not sure why is that..
That sounds odd.  It shouldn't happen.  Can you post an example file? 

Quotealthough indeed it seems there should be an easier way..and faster perhaps because I think it will slow down when it has to iterate through hundreds of images..

Hayo's command should do what you want.  From what I can tell of the code (not familiar with Matlab, so I could be wrong), all you are doing is checking for the existence of a corresponding file with the same name in a different directory.  Hayo's command does this in one shot without looping, so it's much faster.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

christinak

I noticed now that when I rerun the command given by Hayo it does the processing but gives a warning for each image.
Warning: [minor] Possibly incorrect maker notes offsets (fix by 1783?) - C:\..(path of image)

Maybe this has to do something with the Altitude missing decimals?

Indeed that's what I thought the whole loop in Matlab is unnecessary if I can run a simple line from cmd!

Phil Harvey

The warning has nothing to do with Altitude.  See FAQ 15 for a complete explanation.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).