Perl - how to write out thumbnail from a CR2 image

Started by SK, November 29, 2016, 11:45:08 PM

Previous topic - Next topic

SK

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?

Phil Harvey

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";
}
...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 ($).

SK

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.

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!



SK

 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;





Phil Harvey

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
...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 ($).