Extract PreviewImage (not thumbnail)

Added by Albert Shan 28 days ago

I found that JPEG files that come from digital cameras often have an additional higher resolution preview image.

I'm testing some Sony A73 and Fuji X-H1 files, and ExifTool tells me there is a PreviewImage like this:

$ exiftool DSC00507.JPG | grep Prev
Preview Image Size : 1616x1080
Preview Image : (Binary data 1050996 bytes, use -b option to extract)

This is a larger preview image in addition to the thumbnail. The thumbnail is only 160x120 pixels.

I did this in my code:

Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filename);
Exiv2::PreviewManager loader(*image);
Exiv2::PreviewPropertiesList list = loader.getPreviewProperties();

And when I iterate through the PreviewPropertiesList, I only get one item, and that is the thumbnail of the JPEG, but the above PreviewImage at 1610x1080 resolution is missing.

So how do I get the preview image?


Replies (9)

RE: Extract PreviewImage (not thumbnail) - Added by Robin Mills 28 days ago

This is an interesting matter. Exif metadata is stored as a TIFF and embedded in the JPEG. JPEG files consists of segments which are restricted to 64k max. So, thumbnails are usually small so they and the metadata all live within the 64k limit. The Exif specification forbids the writer from referencing data outside the 64k segment. However, I believe some Sony images break the rule. This is not a good idea because it makes it almost impossible to rewrite the file. So any changes to the metadata will probably result in the preview being lost. I can easily imagine that an Engineer at Sony didn't understand the importance of ensuring that the embedded TIFF must not have an external reference because they are not involved with editing metadata.

If you only wish to read the preview, I may be able to provide a workaround. The command $ exiv2 -pR image-path will "print recursively" and enable you to find the "illegal" preview and the utility dd can extract it. The whole thing can be automated into a short bash script.

Another work-around is to use the Raw ARW file and avoid JPEG.

If you could provide a sample image from your camera, I'll be happy to investigate this for you.

The Exiv2 project has moved to https://github.com/exiv2/exiv2. I haven't yet found a way to make our Redmine server "Read only". May I ask you to open a new issue on GitHub about this for two reasons:

1) More folks read and may join this discussion.
2) A member of Team Exiv2 has accepted the challenge BigTiff (64 bit tiff) for Exiv2 v0.28. He'll be very interested in this matter.

A few years ago, I started to investigate these Sony "external" references. That was my first journey into class TiffReader and I didn't have enough knowledge to preserve those previews from JpegBase::writeMetadata(). However since then, I've solved some puzzles in that class TiffReader, and the time may have come to revisit this topic.


However, the most immediate matter is to a good test file.

RE: Extract PreviewImage (not thumbnail) - Added by Albert Shan 28 days ago

Hi Robin,

Thanks for the fast reply. It is not just Sony doing, I think most camera manufacturers are doing something similar.

Here is the link to a Fujifilm JPEG:


Inside, there is a 320x240 preview image, I can extract it with ExifTool with the -PreviewImage switch. But how do I extract that in a C++ program using Exiv2?

I want to do this with Exiv2 for performance reason.


RE: Extract PreviewImage (not thumbnail) - Added by Albert Shan 28 days ago

Here is the link to a Sony A73 JPEG:


Inside, there is a 1610x1080 preview image. Also, I believe all these preview images are JPEGs, not TIFFs.

RE: Extract PreviewImage (not thumbnail) - Added by Robin Mills 28 days ago

Thanks for the files. This is going to take some effort at my end. I added "print recursively/-pR" in Exiv2 v0.26 for several reasons:

1) For debugging files.
2) To help me "really" understand tiff.

The code in class TiffReader is inspirational and really hard to understand. We've been asked to support BigTiff (64 bit tiff) and class TiffReader cannot be refactored without understanding. I'm almost there with understanding. class TiffReader is really really good.

However when I wrote "print recursively/-pR" in 2015, I didn't know how many of the MakerNotes work. They are usually another embedded tiff. I know much more about that now. For sure the current code isn't dealing well with your two files. So, I don't have a work-around for you yet. However I am motivated to work on this.

I'm off to the States tomorrow to visit family and enjoy some winter sunshine in Florida. I will spend time on this. I encourage you to report this on GitHub. I don't expect anybody else will jump into this topic, however that's a more visible place to discuss this important matter.

RE: Extract PreviewImage (not thumbnail) - Added by Albert Shan 27 days ago

I emailed Phil (author of ExifTool), and he said the PreviewImage are stored in MakerNotes, and different manufacturers do this differently.

He told me to try the -htmldump -v2 switches. I did that on the 2 files above and I simply redirect (>) the output to a html file.

Open this html file in a browser, and you can move your mouse to a tag and it'll highlight the data area for you (very nice). It really does help to see what's inside the JPEG file.

I can see that the Sony preview image is in the MPImage2:MPImage data area. The data size confirms it, as it is the same as the preview image size extracted by ExifTool.

The Fuji one is a bit tricky. I think it is embedded in the APP2 FPXR segment. The data in this area closely resemble the size of the preview image size. See:


I'm sure the Fuji Preview image is there, because I can see the JPEG header visually :)

The question is: is there a way for Exiv2 to extract the data in those specific tags? If yes, then it won't be too hard to extract this preview image in the MakerNotes.

RE: Extract PreviewImage (not thumbnail) - Added by Robin Mills 27 days ago

Thanks. Phil often reads the correspondence concerning Exiv2 and offers advice. He's very helpful. And I often use ExifTool's -v5 option to investigate how he is able to locate data that Exiv2 does not find.

Please understand that the Open Source Community gets no assistance from Camera Manufacturers. We have to deal with this by reverse engineering their files.

The question is: is there a way for Exiv2 to extract the data in those specific tags? If yes, then it won't be too hard to extract this preview image in the MakerNotes.

It's not possible today. However I'm going to work on "print recursively/-pR" and get it correctly dump your files.

I can see the APP2 segments in the Fuji file.

617 rmills@rmillsmbp:~/gnu/github/exiv2 $ exiv2 -pS ~/Downloads/Albert/XH1F5365.JPG 
STRUCTURE OF JPEG FILE: /Users/rmills/Downloads/Albert/XH1F5365.JPG
 address | marker       |  length | data
       0 | 0xffd8 SOI  
       2 | 0xffe1 APP1  |   65468 | Exif..II*......................
   65472 | 0xffe2 APP2  |     136 | FPXR........../.F.U.J.I.F.I.L.M
   65610 | 0xffe2 APP2  |      52 | FPXR.......................@...
   65664 | 0xffe2 APP2  |   20864 | FPXR...........................
   86530 | 0xffc4 DHT   |     418 
   86950 | 0xffdb DQT   |     132 
   87084 | 0xffdd DRI   |       4 
   87090 | 0xffc0 SOF0  |      17 
   87109 | 0xffda SOS  
618 rmills@rmillsmbp:~/gnu/github/exiv2 $ 
I live in England. I'm off to the States today to visit family and get some winter sunshine in Florida. I have your files on my laptop and I will investigate this.

RE: Extract PreviewImage (not thumbnail) - Added by Albert Shan 27 days ago

Hi Robin,

I found that Canon does not embed Preview Image in their JPEG (I looked at 5DIII files, ExifTool does not report any preview image). Nikon does, and it seems that the preview image is always in the same place (PreviewIFD:PreviewImage data).

I also learn today that Exiv2 does not read HEIC files. And I don't know how to get it to read a video file, say I want to get the dimension of the video. I've compiled the source with video option turned ON, but still no luck.

Of course I understand this is Open Source stuff, so it is unrealistic to expect a lot of help. But your replies are certainly very insightful.

Anyway, thanks for the help. Please enjoy your holidays with your family and don't worry about this too much until you have more time.


RE: Extract PreviewImage (not thumbnail) - Added by Robin Mills 25 days ago

I'm happy to help you. I'm 68 and retired. Working on Exiv2 keeps my brain active.

HEIC files. The Apple/iOS native format? We don't support that.

Reading video should work. Which version of the code are you using?

RE: Extract PreviewImage (not thumbnail) - Added by Albert Shan 1 day ago

I'm using the latest code.

Yes, HEIC is the Apple iOS native format. ExifTool from Phil Harvey can read the EXIF metadata off of it, but won't allow the values to be edited.

Exiv2 can't read HEIC. I wonder will HEIC be supported ever?

And to Robin: have you had a chance to find out how to extract the preview image yet? It'd be nice if it can be extracted, it'd make creating preview image so much faster than reading the entire JPEG, especially cameras these days have very high MP count.


Redmine Appliance - Powered by TurnKey Linux