Recent

Author Topic: fpExif JFIF section  (Read 371 times)

Romain

  • New Member
  • *
  • Posts: 37
    • Attribute Changer Website
fpExif JFIF section
« on: May 03, 2020, 09:13:06 am »
Hi,

I have a question about adding a new EXIF header into a JPG file.

In fact, I created a sample JPG file in MS Paint and by default it contains basic JFIF tags. When I add a new EXIF header with a DateTimeOriginal tag to the image, the JFIF tags are deleted ( debugged with ExifTool)


[JFIF]   0x0000 JFIF Version        : 1.01
[JFIF]   0x0002 Resolution Unit     : inches
[JFIF]   0x0003 X Resolution        : 96
[JFIF]   0x0005 Y Resolution        : 96


Code: Pascal  [Select][+][-]
  1. ...
  2. L_ImageInfo.LoadFromFile(_IM_FullName);
  3. ...
  4. L_ExifData := L_ImageInfo.CreateExifData;
  5.  
  6. L_Tag := L_ImageInfo.ExifData.AddTagByName('DateTimeOriginal');
  7. TDateTimeTag(L_Tag).AsDateTime := _IM_PhotoDT;
  8.  
  9. L_ImageInfo.SaveToFile(_IM_FullName);    <- JFIF tags get lost after saving
  10. ...
  11.  

Is there a way to keep the JFIF tags when adding a new EXIF header to a file ?

Best regards,
Romain
« Last Edit: May 03, 2020, 10:17:44 am by Romain »

wp

  • Hero Member
  • *****
  • Posts: 7037
Re: fpExif JFIF section
« Reply #1 on: May 03, 2020, 10:35:01 am »
I always thought that the APP0 (JFIF) and APP1 (EXIF) segments are mutually exclusive because each one requires to be first segment after the SOI (start of image) segment (https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format). Why do you need JFIF?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Romain

  • New Member
  • *
  • Posts: 37
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #2 on: May 03, 2020, 11:03:51 am »
It's not that I need to keep JFIF :-)

Based on customer feedback, I did some testing with File Explorer and noticed that it adds a new EXIF section to an existing JFIF to the sample file.

Your link is very informative and it is written that officially both segments are mutually exclusive but that many cameras and applications add both. So, I fully understand your decision to not keep JFIF when an EXIF segment is available or added.  It's something I was not aware.

Thank you !


wp

  • Hero Member
  • *****
  • Posts: 7037
Re: fpExif JFIF section
« Reply #3 on: May 03, 2020, 10:59:34 pm »
Seeking through my images I found that the case of having both JFIF and EXIF segments in the same JPEG file is not so rare. Therefore, I decided to add a new boolean property WriteJFIFandEXIF to TImgInfo which can enforce writing both JFIF and EXIF segments if they exist simultaneously.

Code: Pascal  [Select][+][-]
  1. var
  2.   imgInfo: TImgInfo;
  3.   tag: TTag;
  4.   exif: TExifData;
  5.  
  6. begin
  7.   imgInfo := TImgInfo.Create;
  8.   try
  9.     // Read file
  10.     imgInfo.LoadFromFile('sample.jpg');
  11.  
  12.     // Add EXIF
  13.     exif := ImgInfo.CreateExifData;
  14.  
  15.     // Add Date/Time Tag
  16.     tag := exif.AddTagByName('DateTimeOriginal');
  17.     TDateTimeTag(tag).AsDateTime := now();
  18.  
  19.     // Save to file
  20.     imgInfo.WriteJFIFandEXIF := true;    // Preserve JFIF segment in addition to EXIF
  21.     imgInfo.SaveToFile('edited_image.jpg');
  22.  
  23.   finally
  24.     imgInfo.Free;
  25.   end;  
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Romain

  • New Member
  • *
  • Posts: 37
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #4 on: May 04, 2020, 05:16:35 pm »
Many thanks, much appreciated :-)

However, do you have a link to get the latest snapshot. I'm unable to find the latest snapshot on Sourceforge.

wp

  • Hero Member
  • *****
  • Posts: 7037
Re: fpExif JFIF section
« Reply #5 on: May 04, 2020, 05:53:42 pm »
Use the "Download snapshot" button on https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpexif/. Or use svn.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Romain

  • New Member
  • *
  • Posts: 37
    • Attribute Changer Website
Re: fpExif JFIF section
« Reply #6 on: May 04, 2020, 06:05:29 pm »
New version works perfect.

Thank you again.



 

TinyPortal © 2005-2018