Update preview/thumbnail in CR2 file

Started by martenjansson, April 13, 2012, 05:52:09 AM

Previous topic - Next topic

martenjansson

Hi All,

Total noob so please bare with me...

I just decided to ditch all standalone jpg images from my library. What I want to do is whenever I made some changes to my raw file I want to export it to jpg and then replace the preview image in the Canon CR2 file with this new jpg file. I would like to do this with a perl script.

I've been googling for hours without any luck. Does anyone have some piece of code that shows how to replace the preview image?


Thanks,
Mårten


Phil Harvey

Hi Mårten,

Assuming you have the JPG preview in a variable called $preview, you do this:

use Image::ExifTool;
my $ex = new Image::ExifTool;
$ex->SetNewValue(PreviewImage => $preview);
$ex->WriteInfo($srccr2, $dstcr2);


This will read the file $srccr2 and create a new file $dstcr2 containing the new preview image.  If $dstcr2 is not defined, it will overwrite $srccr2, but make sure you have backups if you do this.

The new preview should be the same dimensions as the original preview.  If it isn't, you should probably also update IFD0:ImageWidth and IFD0:ImageHeight, but this is riskier since it may be more likely to cause problems for raw image readers.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

martenjansson

Thanks Phil!

I spent some more time with Google but can't really get this to work.
What exactly is $preview? A path to a jpg file doesn't work. How do I create this object?



Regards,
Mårten

Phil Harvey

Hi Mårten,

You can do it like this:

my $preview;
open PREVIEW, 'my_image.jpg';
read PREVIEW, $preview, 10000000;
close PREVIEW;


This simple code will work for preview images up to 10000000 bytes.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

martenjansson