Recent

Author Topic: Exiftool re-direct into running program?  (Read 315 times)

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 304
  • My software never cras....
Exiftool re-direct into running program?
« on: July 01, 2022, 04:10:39 pm »

My program calls exiftool[1] via fpSystem and directs the output to a temporary text file.

Code: Pascal  [Select][+][-]
  1.    tempname := /Volumes/Ramdisk/.ABC123ABC123ABC123XYZ34';
  2.   S := 'exiftool' + finame + ' > ' + tempname;
  3.   R := fpSystem(S);
  4.   assign (f,tempname);
  5.   etc;
  6.  
Then the program reads back the file searching for particulars of interest and erases the temporary file.

Question:   Is there a way to do so w/o resorting to a temporary text file or pipes?  Some clever callback perhaps?



[1]Exiftool is a command line utility to examine the exif fields (and change or remove them) of image and video files.
From command line:   exiftool file_of_interest.jpg
                                 exiftool file_of_interest.jpg > result.txt
https://exiftool.org/
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Exiftool re-direct into running program?
« Reply #1 on: July 01, 2022, 04:24:30 pm »
If you can live without the full power of a life-project such as exiftool you could check out the package fpexif which reads EXIF and other meta data in pure Pascal. No need for a temporary file, just create an instance of TImgInfo, load the image file and query a specific tag by its name:
Code: Pascal  [Select][+][-]
  1. var
  2.   imgInfo: TImgInfo;
  3.   tag: TTag;
  4.  
  5. begin
  6.   imgInfo := TImgInfo.Create;
  7.   try
  8.     // Read file
  9.     imgInfo.LoadFromFile('..\test-image.jpg');
  10.  
  11.     // Check for EXIF
  12.     if imgInfo.HasExif then begin
  13.  
  14.       // Write out some tags
  15.       // (1) date and time when the picture was taken
  16.       Write('Date/time: ':20);
  17.       tag := imgInfo.ExifData.TagByName['DateTime'];
  18.       if tag = nil then
  19.         WriteLn('--- not available in this file ---')
  20.       else
  21.         WriteLn(tag.AsString);
  22.  
  23.       // (2) shutter speed used when taking the photo
  24.       tag := imgInfo.ExifData.TagByName['ShutterSpeed'];
  25.       if tag <> nil then
  26.         WriteLn('Shutter speed: ':20, tag.AsString)
  27.       else
  28.       begin
  29.         // (3) Sometimes alternative tags are availabe
  30.         tag := imgInfo.ExifData.TagByName['ExposureTime'];
  31.         if tag <> nil then
  32.           WriteLn('Exposure time: ':20, tag.AsString);
  33.       end;
  34.  
  35.       // Add user comment
  36.       imgInfo.ExifData.TagByName['UserComment'].AsString := 'This is my favorite photo.';
  37.  
  38.       // Save to file
  39.       imgInfo.SaveToFile('edited_image.jpg');
  40.     end
  41.     else
  42.       WriteLn('No EXIF data in this file.');
  43.  
  44.     // Check for IPTC
  45.     if imgInfo.HasIPTC then begin
  46.       // Write out IPTC key words
  47.       Write('Keywords: ':20);
  48.       tag := imgInfo.IptcData.TagByName['Keywords'];
  49.       if tag = nil then
  50.         WriteLn('--- not available in this file ---')
  51.       else
  52.         WriteLn(tag.AsString);
  53.     end
  54.     else
  55.       WriteLn('No IPTC data in this file.');
  56.  
  57.   finally
  58.     imgInfo.Free;
  59.   end;
  60.  
  61.   WriteLn;
  62.   WriteLn('Press ENTER to quit...');
  63.   ReadLn;
  64.  
  65. end.    

Available at https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpexif/ via svn or by downloading the snapshot.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10291
  • FPC developer.
Re: Exiftool re-direct into running program?
« Reply #2 on: July 01, 2022, 05:33:20 pm »
I don't know OS X that well, but you could try

Code: Pascal  [Select][+][-]
  1. uses process;
  2. ....
  3. var readdata,s,tempname:string;
  4. ....
  5.  tempname := /Volumes/Ramdisk/.ABC123ABC123ABC123XYZ34';
  6. S := 'exiftool' + finame
  7. runcommand('/bin/sh',['-c',s],readdata);


AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 304
  • My software never cras....
Re: Exiftool re-direct into running program?
« Reply #3 on: July 01, 2022, 06:16:05 pm »
If you can live without the full power of a life-project such as exiftool you could check out the package fpexif

I don't know OS X that well, but you could try<s>

Thanks gents.  Hopefully the 2nd works, but I'll try fpexif out of curiousity as well.  If it meets the need, will likely be faster IAC.

2nd works on OS X - tests as follows (once I noticed the lack of a blank after "exiftool" ... that was a test, right?)

Code: Pascal  [Select][+][-]
  1. Program TestCommand; uses process;
  2.  
  3. var finame,s:string;
  4. readdata: ansistring;
  5. R:boolean;
  6. BEGIN
  7.    finame := '"/Volumes/Enola/Phots/M-2022-11-17/Airport-002.jpg"';
  8.    S := 'exiftool ' + finame;
  9.    writeln(S);
  10.    r := runcommand('/bin/sh',['-c',s],readdata);
  11.    if r then Writeln(readdata);
  12. END.

I'll try the 1st suggestion { wp } when I get a chance.

Thanks!
« Last Edit: July 01, 2022, 06:40:50 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Exiftool re-direct into running program?
« Reply #4 on: July 01, 2022, 06:31:17 pm »
Tested the following code on Windows. It redirects the output of ExifTool into a memorystream which then can be loaded into a memo:
Code: Pascal  [Select][+][-]
  1. uses
  2.   process;
  3.  
  4. { TForm1 }
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. const
  8.   BUF_SIZE = 2048;
  9.   PICTURE_FILENAME = '..... <name of the picture file> ....';
  10. var
  11.   process: TProcess;
  12.   outputstream: TStream;
  13.   bytesRead: Integer;
  14.   buffer: array[0..BUF_SIZE-1] of byte;
  15. begin
  16.   process := TProcess.Create(nil);
  17.   try
  18.     process.Executable := 'exiftool.exe';
  19.     process.Parameters.Add(PICTURE_FILENAME);
  20.     process.Options := [poUsePipes, poNoConsole];
  21.     process.Execute;
  22.     outputStream := TMemoryStream.Create;
  23.     try
  24.       repeat
  25.         bytesRead := process.Output.Read(buffer, BUF_SIZE);
  26.         OutputStream.Write(buffer, bytesRead)
  27.       until bytesRead = 0;
  28.       outputStream.Position := 0;
  29.       Memo1.Lines.LoadfromStream(outputStream);
  30.     finally
  31.       outputstream.Free;
  32.     end;
  33.   finally
  34.     process.Free;
  35.   end;
  36. end;

P.S.
I see: Marco's RunCommand is more compact (it works on Windows, too)...
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. const
  3.   PICTURE_FILENAME = '..... <name of the picture file> ....';
  4. var
  5.   s: String;
  6. begin
  7.   RunCommand('exiftool.exe', [PICTURE_FILENAME], s, [], swoHide););
  8.   Memo1.Lines.text := s;
  9. end;
« Last Edit: July 01, 2022, 06:49:13 pm by wp »

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 304
  • My software never cras....
Re: Exiftool re-direct into running program?
« Reply #5 on: July 01, 2022, 06:39:25 pm »
@wp - well that's fancier than my version by a long shot!

Thanks.  I'll also try the other code soon.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10291
  • FPC developer.
Re: Exiftool re-direct into running program?
« Reply #6 on: July 01, 2022, 07:29:20 pm »

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 304
  • My software never cras....
Re: Exiftool re-direct into running program?
« Reply #7 on: July 01, 2022, 08:10:43 pm »
P.S.
I see: Marco's RunCommand is more compact (it works on Windows, too)...

... yep - sort of beatya' to it (above). 

Tested as above and improved and added to my program and working fine.

Thanks wp and Marco!
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

 

TinyPortal © 2005-2018