Difference between revisions of "ReNamer:Scripts:Exiv2"

From den4b Wiki
Jump to navigation Jump to search
(first version)
 
(Added a new script using ReNamer 6.9 and Exiv2 0.26)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Scripts}}
 +
 
Script integrates [http://www.exiv2.org/ Exiv2] library in order to extract EXIF/IPTC/XMP tags from many types of images. CRW, CR2, RAW images are also supported.
 
Script integrates [http://www.exiv2.org/ Exiv2] library in order to extract EXIF/IPTC/XMP tags from many types of images. CRW, CR2, RAW images are also supported.
  
 
== Requirements ==
 
== Requirements ==
* ReNamer v5.20 Beta
 
* [http://www.exiv2.org/download.html exiv2.exe] v0.16 in ReNamer's folder
 
  
== Code ==
+
* [http://www.exiv2.org/download.html exiv2.exe] in ReNamer's folder
 +
 
 +
Download and extract Exiv2 package to a temporary location, locate the "bin" folder that contains "exiv2.exe", and copy everything from the "bin" folder into ReNamer's folder.
 +
 
 +
Note that Exiv2 comes in several different build packages, which have different run-time dependencies. If you are experiencing problems with the missing DLLs, it usually means that you are missing the required dependencies. For example, if you are using the Visual Studio variant (e.g. package named <code>exiv2-0.26-msvc.tar.gz</code>), then ensure that you have installed the necessary ''Microsoft Visual Studio / C++ Redistributables''.
 +
 
 +
== Script - Extract tag ==
 +
 
 +
Author: Denis Kozlov. Date: 2018-08-25.
 +
 
 +
Extract a particular tag and insert its content into the filename, as prefix. In this example "Exif.Image.Model" tag is used, see <code>TAG</code> constant.
  
Author: Denis Kozlov. Date: 10 Mar 2008. Extract unformatted line out of the command line output of the exiv2.exe tool. At the moment, this script extracts image timestamp. Adjust EXIV and TAG constants to extract other tags.
+
You can check the list of all available tags by running <code>exiv2.exe -pa <file></code> command. Also, see help via <code>exiv2.exe --help</code> for more options.
  
<pre>
+
* Tested with ReNamer 6.9 and Exiv2 0.26.
{ Insert EXIF/IPTC/XMP tags from images }
 
  
 +
<syntaxhighlight lang="pascal">
 
const
 
const
   EXIV = '"exiv2.exe"';
+
   TAG = 'Exif.Image.Model';
 +
  EXECUTABLE = 'exiv2.exe';
 +
  PARAMETERS = '-Pt -K ' + TAG;
 +
var
 +
  Command, Output: String;
 +
  UnicodeOutput: WideString;
 +
begin
 +
  Command := '"' + EXECUTABLE + '" ' + PARAMETERS + ' "' + FilePath + '"';
 +
  if ExecConsoleApp(Command, Output) = 0 then
 +
  begin
 +
    UnicodeOutput := WideTrim(OemToWide(Output));
 +
    if Length(UnicodeOutput) > 0 then
 +
      FileName := UnicodeOutput + ' ' + FileName;
 +
  end;
 +
end.
 +
</syntaxhighlight>   
 +
 
 +
== Script - Extract tag alternative ==
 +
 
 +
Author: Denis Kozlov. Date: 2013-04-01.
 +
 
 +
Extract unformatted line out of the command line output of the <code>exiv2.exe</code> tool. This sample script extracts ''Image Timestamp'', but you can adjust <code>EXIV</code> and <code>TAG</code> constants to extract other tags.
 +
 
 +
'''Hint:''' ''Image Timestamp'' value is usually formatted as "yyyy:mm:dd hh:mm:ss" which contains an illegal filename character ":". The illegal character can be easily replaced using an additional Replace rule.
 +
 
 +
* Tested with ReNamer 5.74.4 Beta.
 +
 
 +
<syntaxhighlight lang="pascal">
 +
const
 +
  EXE = 'exiv2.exe';
 
   TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
 
   TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
 
+
 
var
 
var
 
   Command, Output: String;
 
   Command, Output: String;
   Matches: TStringsArray;   
+
   Matches: TWideStringArray;   
 
+
 
begin
 
begin
   Command := EXIV+' "'+FilePath+'"';
+
   Command := EXE+' "'+FilePath+'"';
 
   if ExecConsoleApp(Command, Output) = 0 then
 
   if ExecConsoleApp(Command, Output) = 0 then
 
   begin
 
   begin
Line 29: Line 68:
 
   end;
 
   end;
 
end.
 
end.
</pre>
+
</syntaxhighlight>
  
For example, to extract IPTC Headline tag, you would need to make following changes:
+
To extract ''IPTC Headline'' tag, you would need to make following changes:
<pre>
+
<syntaxhighlight lang="pascal">
EXIV = '"exiv2.exe" -pi';
+
EXE = 'exiv2.exe -pi';
 
TAG = 'Iptc.Application2.Headline\s*\w*\s*\d*\s*(.*?)[\r\n]';
 
TAG = 'Iptc.Application2.Headline\s*\w*\s*\d*\s*(.*?)[\r\n]';
</pre>
+
</syntaxhighlight>
 +
 
 +
== Script - Extract EXIF date and reformat ==
 +
 
 +
Author: Denis Kozlov. Date: 2015-11-25.
 +
 
 +
Extract EXIF date and reformat it according to user specification (<code>DATE_FORMAT</code> constant).
 +
 
 +
* Tested with ReNamer 6.3.
 +
 
 +
<syntaxhighlight lang="pascal">
 +
const
 +
  EXIV = 'exiv2.exe';
 +
  DATE_TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
 +
  DATE_PARTS = '(.+)\:(.+)\:(.+)\ (.+)\:(.+)\:(.+)';
 +
  DATE_FORMAT = 'YYYY-MM-DD HH.NN.SS';
 +
 
 +
var
 +
  Command, Output: String;
 +
  Matches, DateParts: TWideStringArray;
 +
  Year, Month, Day, Hours, Minutes, Seconds: Integer;
 +
  Date: TDateTime;
 +
  DateOK: Boolean;
 +
 
 +
begin
 +
  Command := EXIV + ' "' + FilePath + '"';
 +
  if ExecConsoleApp(Command, Output) = 0 then
 +
  begin
 +
    Matches := SubMatchesRegEx(Output, DATE_TAG, False);
 +
    if Length(Matches) > 0 then
 +
    begin
 +
      DateParts := SubMatchesRegEx(Matches[0], DATE_PARTS, False);
 +
      if Length(DateParts) > 0 then
 +
      begin
 +
        DateOK :=
 +
          TryStrToInt(DateParts[0], Year) and TryStrToInt(DateParts[1], Month) and
 +
          TryStrToInt(DateParts[2], Day) and TryStrToInt(DateParts[3], Hours) and
 +
          TryStrToInt(DateParts[4], Minutes) and TryStrToInt(DateParts[5], Seconds);
 +
        if DateOK then
 +
        begin
 +
          Date := EncodeDateTime(Year, Month, Day, Hours, Minutes, Seconds, 0);
 +
          FileName := FormatDateTime(DATE_FORMAT, Date) + WideExtractFileExt(FileName);
 +
        end;
 +
      end;
 +
    end;
 +
  end;
 +
end.
 +
</syntaxhighlight>

Latest revision as of 10:04, 25 August 2018

Script integrates Exiv2 library in order to extract EXIF/IPTC/XMP tags from many types of images. CRW, CR2, RAW images are also supported.

Requirements

Download and extract Exiv2 package to a temporary location, locate the "bin" folder that contains "exiv2.exe", and copy everything from the "bin" folder into ReNamer's folder.

Note that Exiv2 comes in several different build packages, which have different run-time dependencies. If you are experiencing problems with the missing DLLs, it usually means that you are missing the required dependencies. For example, if you are using the Visual Studio variant (e.g. package named exiv2-0.26-msvc.tar.gz), then ensure that you have installed the necessary Microsoft Visual Studio / C++ Redistributables.

Script - Extract tag

Author: Denis Kozlov. Date: 2018-08-25.

Extract a particular tag and insert its content into the filename, as prefix. In this example "Exif.Image.Model" tag is used, see TAG constant.

You can check the list of all available tags by running exiv2.exe -pa <file> command. Also, see help via exiv2.exe --help for more options.

  • Tested with ReNamer 6.9 and Exiv2 0.26.
const
  TAG = 'Exif.Image.Model';
  EXECUTABLE = 'exiv2.exe';
  PARAMETERS = '-Pt -K ' + TAG;
var
  Command, Output: String;
  UnicodeOutput: WideString;
begin
  Command := '"' + EXECUTABLE + '" ' + PARAMETERS + ' "' + FilePath + '"';
  if ExecConsoleApp(Command, Output) = 0 then
  begin
    UnicodeOutput := WideTrim(OemToWide(Output));
    if Length(UnicodeOutput) > 0 then
      FileName := UnicodeOutput + ' ' + FileName;
  end;
end.

Script - Extract tag alternative

Author: Denis Kozlov. Date: 2013-04-01.

Extract unformatted line out of the command line output of the exiv2.exe tool. This sample script extracts Image Timestamp, but you can adjust EXIV and TAG constants to extract other tags.

Hint: Image Timestamp value is usually formatted as "yyyy:mm:dd hh:mm:ss" which contains an illegal filename character ":". The illegal character can be easily replaced using an additional Replace rule.

  • Tested with ReNamer 5.74.4 Beta.
const
  EXE = 'exiv2.exe';
  TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
 
var
  Command, Output: String;
  Matches: TWideStringArray;  
 
begin
  Command := EXE+' "'+FilePath+'"';
  if ExecConsoleApp(Command, Output) = 0 then
  begin
    Matches := SubMatchesRegEx(Output, TAG, False);
    if Length(Matches) > 0 then
      FileName := Matches[0] + WideExtractFileExt(FileName);
  end;
end.

To extract IPTC Headline tag, you would need to make following changes:

EXE = 'exiv2.exe -pi';
TAG = 'Iptc.Application2.Headline\s*\w*\s*\d*\s*(.*?)[\r\n]';

Script - Extract EXIF date and reformat

Author: Denis Kozlov. Date: 2015-11-25.

Extract EXIF date and reformat it according to user specification (DATE_FORMAT constant).

  • Tested with ReNamer 6.3.
const
  EXIV = 'exiv2.exe';
  DATE_TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
  DATE_PARTS = '(.+)\:(.+)\:(.+)\ (.+)\:(.+)\:(.+)';
  DATE_FORMAT = 'YYYY-MM-DD HH.NN.SS';

var
  Command, Output: String;
  Matches, DateParts: TWideStringArray;
  Year, Month, Day, Hours, Minutes, Seconds: Integer;
  Date: TDateTime;
  DateOK: Boolean;

begin
  Command := EXIV + ' "' + FilePath + '"';
  if ExecConsoleApp(Command, Output) = 0 then
  begin
    Matches := SubMatchesRegEx(Output, DATE_TAG, False);
    if Length(Matches) > 0 then
    begin
      DateParts := SubMatchesRegEx(Matches[0], DATE_PARTS, False);
      if Length(DateParts) > 0 then
      begin
        DateOK :=
          TryStrToInt(DateParts[0], Year) and TryStrToInt(DateParts[1], Month) and
          TryStrToInt(DateParts[2], Day) and TryStrToInt(DateParts[3], Hours) and
          TryStrToInt(DateParts[4], Minutes) and TryStrToInt(DateParts[5], Seconds);
        if DateOK then
        begin
          Date := EncodeDateTime(Year, Month, Day, Hours, Minutes, Seconds, 0);
          FileName := FormatDateTime(DATE_FORMAT, Date) + WideExtractFileExt(FileName);
        end;
      end;
    end;
  end;
end.