question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write simple Exif metadata to JPEG from scratch.

See original GitHub issue

I don’t understand how to write simple Exif metadata to a JPEG file I’ve read using Java Image I/O.

I’m successfully reading, processing, and writing JPEG metadata using Java Image I/O. It’s works fine.

I set my image reader to ignore reading metadata. I don’t care about the original metadata (in this context). Indeed the original JPEG may have already had its metadata removed, and have no metadata whatsoever:

ImageInputStream imageInputStream= …;
ImageReader imageReader = …;
ImageReadParam imageReadParam = imageReader.getDefaultReadParam();
imageReader.setInput(imageInputStream, true, true);
Buffered Image image = imageReader.read(0, imageReadParam);

I write it out:

ImageWriter imageWriter = getImageWriter(imageReader);
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
…
final ImageOutputStream imageOutputStream = …;
imageWriter.setOutput(imageOutputStream);
IIOImage iioImage = new IIOImage(newImage, null, null);
imageWriter.write(null, iioImage, imageWriteParam);

This gives me no metadata, as I would expect.

Now with TwelveMonkeys, I want to add the simplest of the very simplest Exif metadata to my JPEG. Let’s say I want to add the Exif field ImageDescription (270, 0x010E) with a value of “foobar”. What could be simpler? But I don’t see how to do that with TwelveMonkeys.

In How to write Exif to a JPEG with TwelveMonkeys’ ExifWriter class, @haraldk mentioned some sort of super low-level approach that involved actually serializing strings to bytes. I’m not sure how this relates to the TwelveMonkeys EXIFWriter/TIFFWriter, as I see that they seem to do serializing internally.

But the bigger issue is how to get an IIOMetadata instance. If I had one of those, I could just pass it to new IIOImage(newImage, null, iioMetadata) couldn’t I? I see that there is a JPEGImage10Metadata class, but how do I get one of those? Moreover how do I do this in a plugin-agnostic way, so that I don’t tie the code to TwelveMonkeys?

I’m a little lost here. Could someone point me in the right direction? Surely writing a single Exif “foobar” description value from scratch is the simplest Exif test I could make, but I can’t even figure out how to do that.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
garretwilsoncommented, Apr 25, 2021

However it appears that Apache Commons Imaging can’t even write two simple Exif values to a JPEG without corrupting them!

I wanted to circle back and note that I was mistaken on this point. As I explained in IMAGING-281, the Exif property in question was a special Windows property that instead of using UTF-8 required UCS-2 to be used. That’s why a single tag seemed to “corrupted” while the others were working.

So Apache Commons Imaging is able to write metadata from scratch to a JPEG, although it requires rewriting an existing image. For now this is working for me, although it would be preferable at some point to add the metadata dynamically while I am processing it using Java Image I/O.

0reactions
haraldkcommented, Apr 25, 2021

Closing for now. Might add something to the Wiki later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Read and Remove Metadata from Your Photos ... - Auth0
How to Read and Remove Metadata from Your Photos With Python. Smartphones include EXIF metadata in their photos. Here's how to read, write,...
Read more >
R write EXIF data to JPEG file - Stack Overflow
Install ExifTool, I use Ubuntu comand: sudo apt install libimage-exiftool-perl. Then in my R code, to add GPS coordinates to image I use:...
Read more >
How to Obtain Valuable Data from Images Using Exif Extractors
To view Exif information, upload a file or add its public URL, then hit "View Exif." In my example, passing the same photo...
Read more >
How to Edit Image Metadata with Exiftool - YouTube
Learn how to read, change and add just about any attribute on an image with the infamous exiftool software. Want another take or...
Read more >
Extract iPhone and Android EXIF metadata from ... - YouTube
Can you find out the locations of my photos? Put it in the comments below.You need to learn to code! Learn Python. You...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found