Project

General

Profile

What if an image has no previews? [OSX]

Added by Patrick Hennessey over 3 years ago

If no preview is found after running the -pp option, is there a method to force OSX to generate a preview thumbnail of an image? I have a bunch of TIF and JPG files that result in no reply when trying to extract a preview.


Replies (5)

RE: What if an image has no previews? [OSX] - Added by Robin Mills over 3 years ago

Patrick

At the moment, exiv2 can only read previews in images. You cannot modify, delete, or add previews to images. There is a project being discussed to add this capability, however it's still in early stages of discussion by private email.

The storage of thumbnails is complex. It's not a single tag and there can be multiple thumbnails at different resolutions. Additionally there can be thumbnails embedded in the MakerNote. And for even more complexity, different formats (TIFF, JPEG, PNG etc) can store thumbnails in format specific ways which are not part of the Exif standard.

You can download my favourite test file from http://clanmills.com/Stonehenge.jpg

There are a couple of tags explained on page 24 (physically p29) of the Exif standard: http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

We can manually extract the thumbnail from the image as follows:

$ exiv2 -pa --grep JPEG ~/Stonehenge.jpg
Exif.Thumbnail.JPEGInterchangeFormat         Long        1  4442    <--- location of thumbnail
Exif.Thumbnail.JPEGInterchangeFormatLength   Long        1  10837   <--- length   of thumbnail
$ exiv2 -pS ~/Stonehenge.jpg                 # examine Structure of image file
STRUCTURE OF JPEG FILE: /Users/rmills/Stonehenge.jpg
 address | marker       |  length | data
       0 | 0xffd8 SOI  
       2 | 0xffe1 APP1  |   15288 | Exif..II*...................... <--- location of the exif data
   15292 | 0xffe1 APP1  |    2604 | http://ns.adobe.com/xap/1.0/.<?x
   17898 | 0xffed APP13 |      96 | Photoshop 3.0.8BIM.......'.....
   17996 | 0xffe2 APP2  |    4094 | MPF.II*...............0100.....
   22092 | 0xffdb DQT   |     132 
   22226 | 0xffc0 SOF0  |      17 
   22245 | 0xffc4 DHT   |     418 
   22665 | 0xffda SOS  
$ 
We extract the exif data. The segment is 2 bytes into the file and its marker is 4 bytes (2 byte marker and 2 byte length). There are 6 leading bytes ("Exif\0\0") in the segment.
$ dd bs=1 skip=$((2+4+6)) count=15288 if=~/Stonehenge.jpg > Stonehenge.exif
15288+0 records in
15288+0 records out
15288 bytes transferred in 0.164319 secs (93039 bytes/sec)
$ dd bs=1 skip=4442 count=10837 if=~/Stonehenge.exif | exiv2 -      # extract thumbnail and pipe through exiv2
10837+0 records in
10837+0 records out
10837 bytes transferred in 0.039951 secs (271257 bytes/sec)
File name       : -
MIME type       : image/jpeg
Image size      : 160 x 120
-: No Exif data found in the file
$ 
As you can see, this isn't trivial. The reverse process (to write the tags/binary thumbnail) is formidable. It's unlikely that you can add/del/mod exif thumbnail until we implement thumbnail editing in Exiv2.

Incidentally the exif data is an embedded tiff:

$ exiv2 Stonehenge.exif                                             # exif data is a TIFF !
File name       : Stonehenge.exif
MIME type       : image/tiff                                        <--- It's a TIFF!
...
Thumbnail       : image/jpeg, 10837 Bytes                           <--- Thumbnail in embedded tiff!
...
$ 

I suspect there is a simpler way to write a thumbnail directly into a segment in a JPEG (not embedded in the Exif data), however I have to do more research to understand this.

RE: What if an image has no previews? [OSX] - Added by Gilles Caulier over 3 years ago

Preview can be written in image with a standard : IPTC. A lots of application use it already:

https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html#ApplicationRecord

...search ObjectPreviewFileFormat tag to see all the format supported.

The preview image must be JPEG for interroperability. The IPTC tag relevant said that image must be limited to 256Ko, but With JPEG container, this break the IPTC metadata SEGMENT which is limited to ... 64Ko. There is no limitation problem with other image format, as TIFF, JPEG2000, or PNG.

If you know this limitation, you can use Iptc.preview tag. We use it in digiKam for all image formats supporting Iptc. When an image edit is done, we record a medium preview image stored in Iptc to render quickly the image while a slide show for ex.

Originally, i thinking to use another image format as preview, as something like wavelets based preview to optimize size and quality, but this will work only in DK, not outside.

https://cgit.kde.org/digikam.git/tree/libs/dmetadata/metaengine_image.cpp#n1059

The same can be certainly done in Xmp, but i never checked this point, as Iptc do the stuff very well. After all, in DK we have a dedicated XMP namespace where a specific tag can be created to store a wavelets preview binary array...

Gilles Caulier

RE: What if an image has no previews? [OSX] - Added by Robin Mills over 3 years ago

Thank you Gilles for this insight. I suspected there could be other ways to achieve a successful result. Thanks for contributing to this discussion.

RE: What if an image has no previews? [OSX] - Added by Patrick Hennessey over 3 years ago

Thanks for the insight! To Gilles, is there another terminal command that will force OSX to generate a preview for a particular image file that exiv2 cannot find a thumbnail for?

RE: What if an image has no previews? [OSX] - Added by Robin Mills over 3 years ago

Patrick:

I'll leave Gilles to answer your question in his own way. And I hope he'll give you the exiv2 command to insert a thumbnail into XMP or IPTC metadata.

I'd implement this as a bash script. I'd enumerate the previews with exiv2 and if there are none, I'd create a thumbnail with OSX sips command and use Gilles magic to insert the thumbnail into the image as XMP or IPTC.

You can count the number of previews in an image with exiv2 and wc:

$ exiv2 -pp ~/Stonehenge.jpg | wc -l
1
$ exiv2 -pp ~/gnu/github/exiv2/exiv2/test/data/Reagan.tiff | wc -l
0
$ 
The sips commands to create a thumbnail are:
$ cp ~/Stonehenge.jpg /tmp
$ exiv2 -pa --grep dimension/i /tmp/Stonehenge.jpg 
Exif.Photo.PixelXDimension                   Short       1  6000
Exif.Photo.PixelYDimension                   Short       1  4000
$ sips --resampleHeightWidthMax 200 /tmp/Stonehenge.jpg 
/private/tmp/Stonehenge.jpg
<CGColor 0x7fbfe5e12780> [<CGColorSpace 0x7fbfe5e1d2c0> (kCGColorSpaceDeviceRGB)] ( 0 0 0 1 )
  /private/tmp/Stonehenge.jpg
$ exiv2 -pa --grep dimension/i /tmp/Stonehenge.jpg 
Exif.Photo.PixelXDimension                   Long        1  200
Exif.Photo.PixelYDimension                   Long        1  133
$ 

    (1-5/5)