ExifTool Forum

ExifTool => Developers => Topic started by: SK on November 29, 2016, 11:45:08 PM

Title: Perl - how to write out thumbnail from a CR2 image
Post by: SK on November 29, 2016, 11:45:08 PM
Hi I am aware from the command line this works

exiftool -thumbnailimage -b image.cr2 > thumb.jpg


Can you give me an example in perl how I can get the same functionality?
Title: Re: Perl - how to write out thumbnail from a CR2 image
Post by: Phil Harvey on November 30, 2016, 12:14:55 AM
use Image::ExifTool;
my $infile = "test.jpg";
my $et = new Image::ExifTool;
my $info = $et->ImageInfo($infile, "ThumbnailImage");
if ($$info{ThumbnailImage}) {
    open OUT, ">thumb.jpg";
    print OUT ${$$info{ThumbnailImage}};
    close OUT;
} else {
    warn "$infile doesn't contain a ThumbnailImage\n";
}
Title: Re: Perl - how to write out thumbnail from a CR2 image
Post by: SK on December 02, 2016, 08:52:55 AM
Hi Phil,

Here is the code snippet


use Image::ExifTool;
my $et = new Image::ExifTool;
my $infile = "2016-05-17-00001.CR2";
my $info = $et->ImageInfo($infile, "ThumbnailImage");
if ($$info{ThumbnailImage}) {
    open OUT, ">Thumb.jpg";
    print OUT ${$$info{ThumbnailImage}};
    close OUT;
} else {
    warn "$infile doesn't contain a ThumbnailImage\n";
}


This does create a Thumb.jpg with 8 kb but unable to open it and view the same in standard programs or windows. Any suggestions?

However changed the word "ThumbnailImage" to "Thumbnailimage" [ i in image is lower case per https://metacpan.org/pod/distribution/Image-ExifTool/lib/Image/ExifTool.pod (https://metacpan.org/pod/distribution/Image-ExifTool/lib/Image/ExifTool.pod).

Now I get the message
Quote2016-05-17-00001.CR2 doesn't contain a Thumbnailimage

I am assuming all CR2 (canon raw) images have a thumbnail!


Title: Re: Perl - how to write out thumbnail from a CR2 image
Post by: SK on December 02, 2016, 03:24:42 PM
 Ok I got it working....however the jpg image is in landscape orientation (not identical to original image). Is there a way I can rotate left?

This is the code that worked:


use Image::ExifTool qw(ImageInfo);

my $exifTool = new Image::ExifTool;

my $info = $exifTool->ImageInfo('2016-05-17-00001.CR2', 'thumbnailimage');

open OUT, ">thumb.jpg";
binmode OUT;                       ###searched your forum and you had suggested this :-)
print OUT ${$$info{ThumbnailImage}};
close OUT;




Title: Re: Perl - how to write out thumbnail from a CR2 image
Post by: Phil Harvey on December 02, 2016, 10:06:06 PM
Try copying the Orientation tag from the CR2 to the thumbnail:

use Image::ExifTool qw(ImageInfo);
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo('2016-05-17-00001.CR2', 'thumbnailimage');
my $thumb = ${$$info{ThumbnailImage}};
$exifTool->SetNewValue(Orientation => $$info{Orientation});
$exifTool->WriteInfo(\$thumb, 'thumb.jpg');


I don't have time to test this, but it should do everything you want, including writing the output "thumb.jpg" file.

- Phil