Thumbnail generation via perl API

Started by dfreem01, January 05, 2011, 02:15:55 PM

Previous topic - Next topic

dfreem01

Howdy,

I've searched and been unable to find a method by which to extract the thumbnail via the perl API instead of the command line.  I've seen how to extract thumbnail info here:

Extract information from an embedded thumbnail image:
$info = ImageInfo('image.jpg', 'thumbnailimage');
my $thumbInfo = ImageInfo($$info{ThumbnailImage});


Surely I'm missing it completely.

Additionally, this is a fantastic tool (and I haven't even implemented it yet!).  I have thousands of pictures to tag and organize and I'm looking forward to making an extensible framework with this as the base.

Thanks!

David

Phil Harvey

Hi David,

You've got it right there in your script.  After the first call to ImageInfo(), $$info{ThumbnailImage} contains a SCALAR reference to the JPEG thumbnail image.  To write this to file, you could do this:

open OUT, ">thumb.jpg";
binmode OUT;
print OUT ${$$info{ThumbnailImage}};
close OUT;


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

jcsdoc

Hi everyone,

I am storing image metadata in mySQL using perl and exiftool, when I try to store the thumbnailimage in a blob field I get only a scalar reference e.g. SCALAR(0x2b139f0).
I tried to activate the Binary option without luck.

Any ideas?

thx

Phil Harvey

You must dereference the SCALAR reference.  Add an extra "$" to dereference the variable as in my post above.

ie. ${ some scalar reference }

The Binary option doesn't do the dereferencing for you.  What it does is ensure that the data is extracted.

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

jcsdoc

Thank you very much for your answer Phil.

I am still doing something wrong (obviously), even if I managed to store the thumbnail image in the blob field, the data seems to be corrupted.

Here is the header of generated thumbnails:

0000000: 27ff d8ff e05c 3010 4a46 4946 5c30 0101  '....\0.JFIF\0..
0000010: 5c30 5c30 015c 3001 5c30 5c30 ffdb 5c30  \0\0.\0.\0\0..\0
0000020: 435c 3002 0202 0202 0102 0202 0202 0202  C\0.............
0000030: 0303 0604 0303 0303 0705 0504 0608 0708  ................


Here is how I store it:


if (defined $info->{ThumbnailImage}) {
   my $binary =  ${$$info{ThumbnailImage}};
   my $qry_update_blob = "UPDATE exif SET ThumbnailImage = ? WHERE id = $last;";
   my $stm_update_blob = $dbh->prepare($qry_update_blob);
   $stm_update_blob->execute($dbh->quote($binary));;
   }



Here is how I read it:


my $qry_test = "SELECT ThumbnailImage, FileName, id FROM exif";
my $stm_test = $dbh->prepare($qry_test);
$stm_test->execute();

while (my @DATA = $stm_test->fetchrow_array) {
            
      if (defined $DATA[0]) {
         $DATA[1] =~ /\.(.*)$/;      
         my $file = "./test/$DATA[2].$1";
         open THUMB, ">$file" or die $!;
         binmode THUMB or die $!;
         print THUMB $DATA[0];
      close THUMB;
   }
}



Thank you.

Phil Harvey

I can't help you with this.  I don't even know what type of object $dbh is.  It seems like some sort of database, but I have no experience with database modules in Perl.  However, I would be wary of the $dbh->quote() call.

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

jcsdoc

Dear Phil,

Removing the $dbh->quote() call solved the issue (there are n00bs running Linux cf. ExifTool For Dummies)
Now the thumbnails are generated correctly and the md5 match between thumbnails generated from ImageInfo directly and the ones stored in the db and generated later.

Thank you very much for your invaluable work, your quick replies ... and sorry for asking a question that was actually not related to ExifTool itself.

Best regards.

J.