Skip to main content

Editing JPEG Exif and thumbnails

7 replies [Last post]
alie
Offline
Joined: 2005-03-31

Hi,

I'm trying to update a jpeg with GPS Exif data. So far, with help from posts here, I can successfully copy the Exif data complete with the new GPS information over to a new copy of the image. I read on this list that performing the edit in place wouldn't be possible and anyway I don't really want to go editing the metadata on the original so this is fine, in spite of the loss of quality to the image.

To do this I needed to use the Java SE Jpeg reader/writer because the imageio reader and writer (CLibJPEGImageReader/Writer) doesn't seem to allow editing the metadata (as far as I can see CLibJPEGMetadata has isReadOnly always set to true). Since I use the metadata from the original image as the metadata for the copy I need to be able to edit it (e.g. with setFromTree or mergeTree). I don't see any other way to obtain an editable copy. Any suggestions would be greatly appreciated.

The main problem I had in doing this was with the imageio TIFF reader and writer that I used for actually editing the Exif and building the new App1 node. I would get a class cast exception if I used a TIFFDirectory directly to store the GPS IFD. It seems that internally it assumes the type of the IFD will be TIFFIFD which is a sub-class of TiffDirectory (see line 461 in TIFFIFD - TIFFIFD subIFD = (TIFFIFD)f.getData();). TIFFIFD isn't documented in the standard API so I would take this to be a bug. At the least in the documentation because it isn't possible to work out how to create an IFD using that alone, but also presumably in the code too because it doesn't seem like the TIFFIFD class is intended to be exposed in the API.

The problem I have now is how to copy over the thumbnail data. For the three camera makes I've tried two store the thumbnail in the Exif on the App1 segment (the other has the error "ICC APP2 encountered without prior JFIF!" which is a different problem). The SE Jpeg reader doesn't find these because it only looks at the JFIF one. The imageio one does but then I can't then edit the metadata with this. I'd rather not read the image twice with both readers.

I am able to find the thumbnail using the TIFF reader when reading the Exif, it's the next image (compressed as a JPEG or OLD JPEG). Ive tried both giving this BufferedImage to the Tiff writer as a thumbnail when it's writing the new App1 node and to the Jpeg writer as a thumbnail when it's writing the new image but in both cases the final image doesn't have a thumbnail. Ideally I'd like the thumbnail to be stored in exactly the same place in the copy as the original (i.e. the Exif meta data) . Does anyone know how I might achieve this? I don't claim to be an accomplished programmer so I'm probably missing something obvious. Since it might be important I'm using a tool I'm using a tool ExifReader to check the meta data and thumbnail is there.

Regards, Alistair

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
phil_butt2000
Offline
Joined: 2007-06-14

This is something of interest too my work as well...if anybody can suggest a good fix I would be very grateful.

Thanks
PHil

simg
Offline
Joined: 2005-01-20

Dear alie:
Is that possible for you to share the code for GPS info updating part as reference?
Thanks in advance!

Simon Gong
mail: simg@keysurf.net

alie
Offline
Joined: 2005-03-31

I've uploaded a jar file containing the sources (2 classes) and an example jpeg image with the result (prefixed with GPS_). It can be tested from the main method of ExifWriter. I use the exifreader software to verify the exif ( http://www.takenet.or.jp/~ryuuji/minisoft/exifread/english/ )

the jar of mine is here http://rapidshare.com/files/35935266/gpsExifWriter.jar

I still haven't fixed the problem, which occurs independently of whether I add the GPS metadata or not.

I hope this helps someone identify the problem and to code there own work.

simg
Offline
Joined: 2005-01-20

thanks a lot!

bpb
Offline
Joined: 2004-06-23

I believe that this procedure is completely documented in the javadoc of the TIFF package

http://download.java.net/media/jai-imageio/javadoc/1.1/com/sun/media/ima...

in particular in these sections:

http://download.java.net/media/jai-imageio/javadoc/1.1/com/sun/media/ima...
http://download.java.net/media/jai-imageio/javadoc/1.1/com/sun/media/ima...

If this is not the case please apprise us of the deficiency.

> Hi,
>
> I'm trying to update a jpeg with GPS Exif data. So
> far, with help from posts here, I can successfully
> copy the Exif data complete with the new GPS
> information over to a new copy of the image. I read
> on this list that performing the edit in place
> wouldn't be possible and anyway I don't really want
> to go editing the metadata on the original so this is
> fine, in spite of the loss of quality to the image.
>
> To do this I needed to use the Java SE Jpeg
> reader/writer because the imageio reader and writer
> (CLibJPEGImageReader/Writer) doesn't seem to allow
> editing the metadata (as far as I can see
> CLibJPEGMetadata has isReadOnly always set to true).
> Since I use the metadata from the original image as
> the metadata for the copy I need to be able to edit
> it (e.g. with setFromTree or mergeTree). I don't see
> any other way to obtain an editable copy. Any
> suggestions would be greatly appreciated.
>
> The main problem I had in doing this was with the
> imageio TIFF reader and writer that I used for
> actually editing the Exif and building the new App1
> node. I would get a class cast exception if I used a
> TIFFDirectory directly to store the GPS IFD. It seems
> that internally it assumes the type of the IFD will
> be TIFFIFD which is a sub-class of TiffDirectory (see
> line 461 in TIFFIFD - TIFFIFD subIFD =
> (TIFFIFD)f.getData();). TIFFIFD isn't documented in
> the standard API so I would take this to be a bug. At
> the least in the documentation because it isn't
> possible to work out how to create an IFD using that
> alone, but also presumably in the code too because it
> doesn't seem like the TIFFIFD class is intended to be
> exposed in the API.
>
> The problem I have now is how to copy over the
> thumbnail data. For the three camera makes I've tried
> two store the thumbnail in the Exif on the App1
> segment (the other has the error "ICC APP2
> encountered without prior JFIF!" which is a different
> problem). The SE Jpeg reader doesn't find these
> because it only looks at the JFIF one. The imageio
> one does but then I can't then edit the metadata with
> this. I'd rather not read the image twice with both
> readers.
>
> I am able to find the thumbnail using the TIFF reader
> when reading the Exif, it's the next image
> (compressed as a JPEG or OLD JPEG). Ive tried both
> giving this BufferedImage to the Tiff writer as a
> thumbnail when it's writing the new App1 node and to
> the Jpeg writer as a thumbnail when it's writing the
> new image but in both cases the final image doesn't
> have a thumbnail. Ideally I'd like the thumbnail to
> be stored in exactly the same place in the copy as
> the original (i.e. the Exif meta data) . Does anyone
> know how I might achieve this? I don't claim to be an
> accomplished programmer so I'm probably missing
> something obvious. Since it might be important I'm
> using a tool I'm using a tool ExifReader to check the
> meta data and thumbnail is there.
>
> Regards, Alistair

alie
Offline
Joined: 2005-03-31

Thanks for your response. I'd followed the instructions almost exactly and now I've rechecked and can't see any problems. But still the thumbnail isn't there.

What is particularly strange is that the thumbnail metadata is quite messed up in the copy in particular the X and YResolution. It reads

Original

Compression : OLDJPEG
XResolution : 72/1
YResolution : 72/1
ResolutionUnit : Inch
JPEGInterchangeFormat : 25045
JPEGInterchangeFormatLength : 6137

New copy

Compression : OLDJPEG
XResolution : 4390913/16843009
YResolution : 16843009/16843009
ResolutionUnit : Inch
JPEGInterchangeFormat : 25360
JPEGInterchangeFormatLength : 11118

If I also compress the thumbnail when writing it out, the metadata it becomes

ImageWidth : 160
ImageHeight : 120
BitsPerSample : 67,1,257
Compression : JPEG
PhotometicInterpretation : YCbCr
StripOffset : 16843009,16843009,16843265,16843009,33686017,33751555,50528771,50529028
SamplesPerPixel : 3
RowsPerStrip : 16
StripByteCounts : 67306244,50529028,84149252,67437829,50595078,84215300,84215295,-620739839
XResolution : 16843009/16843265
YResolution : 16909571/50529541
ResolutionUnit : Inch
JPEGInterchangeFormat : 25612

Does this shed any light?

Regards, Alistair

Message was edited by: alie

Message was edited by: alie

bpb
Offline
Joined: 2004-06-23

Well from this information I don't know.

Do you suppose you could post a self-contained test case which reproduces the problem?

> Thanks for your response. I'd followed the
> instructions almost exactly and now I've rechecked
> and can't see any problems. But still the thumbnail
> isn't there.
>
> What is particularly strange is that the thumbnail
> metadata is quite messed up in the copy in particular
> the X and YResolution. It reads
>
> Original
>
> Compression : OLDJPEG
> XResolution : 72/1
> YResolution : 72/1
> ResolutionUnit : Inch
> JPEGInterchangeFormat : 25045
> JPEGInterchangeFormatLength : 6137
>
> New copy
>
> Compression : OLDJPEG
> XResolution : 4390913/16843009
> YResolution : 16843009/16843009
> ResolutionUnit : Inch
> JPEGInterchangeFormat : 25360
> JPEGInterchangeFormatLength : 11118
>
> If I also compress the thumbnail when writing it out,
> the metadata it becomes
>
> ImageWidth : 160
> ImageHeight : 120
> BitsPerSample : 67,1,257
> Compression : JPEG
> PhotometicInterpretation : YCbCr
> StripOffset :
> 16843009,16843009,16843265,16843009,33686017,33751555,
> 50528771,50529028
> SamplesPerPixel : 3
> RowsPerStrip : 16
> StripByteCounts :
> 67306244,50529028,84149252,67437829,50595078,84215300,
> 84215295,-620739839
> XResolution : 16843009/16843265
> YResolution : 16909571/50529541
> ResolutionUnit : Inch
> JPEGInterchangeFormat : 25612
>
> Does this shed any light?
>
> Regards, Alistair
>
> age was edited by: alie
>
> Message was edited by: alie