How do you copy jpeg metadata (in java) from one image file to another efficiently? [closed]
Asked Answered
V

1

-2

Suppose I have a jpg image file 1.jpg.
I make a copy of it named 1copy.jpg.
(now i have 2 copies of the same image)

Then several changes are made independently to EXIF attributes of the two images so now both have different values, and possibly different EXIF directory structure.

Now, my goal is to copy all the EXIF metadata of 1.jpg to 1copy.jpg so that afterwards the two files are identical (ignoring modification dates). How can this be done in Java, without copying the file again and without rewriting all the sections of 1copy.jpg?

Invoking ExifTool for example won't do the trick as it will rewrite the whole file.

Some Java libraries I looked at, copy the content of metadata attributes but the overall EXIF directory structure of the files may be different and the files would end up containing the same metadata attributes and values, but will not be equivalent byte-for-byte.

So the way I was thinking of doing this is to open file 1.jpg in binary mode, find the EXIF section (starts with FF1E) in both files. Copy byte for byte the whole EXIF section (and only that section) from the source to the destination file - i.e. replace the EXIF section in place, without rewriting any other section of the file.

If this approach is fine, is there any library that allows doing that?

Second question: I also want to do that to mp4 files. Can the same strategy be used?

Third question: just curious... When you make a change to an EXIF property in windows Explorer (e.g. Title) does Windows rewrite the whole file or does it just update the entry in the EXIF directory section of the file?

Vladi answered 23/6, 2022 at 13:9 Comment(1)
A user with your membership time and reputation should understand that A) asking multiple questions and B) requesting library recommendations renders your question off topic here. Especially when your 3 question is a pure "how does tool X work" inquiry that has nothing to do with programming at all.Valiancy
P
4

The Exif segment of a JPEG is variable length, and contains a TIFF structure. While it's technically possible to change Exif (TIFF) data without rewriting the container file, this limits you to change only a selection of fields, or be very careful so that strings don't grow etc.. In practice, this is not very useful.

Instead, I think most software that edits Exif metadata will replace the entire Exif segment, and copy the other segments from the original file as-is. As the JPEG interchange format is based on segments, this is relatively easy to do.

I believe the situation is similar for the MP4 format.

Something like (pseudo code):

file1 // original
file2 // modified copy
file3 // the new output

for each segment in file1
   if segment is exif
       copy exif from file2 to file3
   else 
       write segment to file3

 rename file3 to file1

So, yes, if this your plan, that should work (it will still technically "rewrite the whole file" though).

I have made a few low-level metadata classes with similar functionality that could be helpful as a starting point, but don't provide this out of the box. See JPEGSegmentUtil and JPEGSegmentImageInputStream.

However, if you goal is that the files will be identical after the operation, just copying the file seems a lot simpler and safer.


PS: It's also worth noting that some fields in the Exif data is directly related to the interpretation of the image data (color space, orientation, dimensions). So you should probably copy only a subset of fields, or make sure the image data is otherwise the same.

Parulis answered 24/6, 2022 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.