ExifTool Forum

ExifTool => The Image::ExifTool API => Topic started by: dfreem01 on January 05, 2011, 02:15:55 PM

Title: Thumbnail generation via perl API
Post by: dfreem01 on January 05, 2011, 02:15:55 PM
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
Title: Re: Thumbnail generation via perl API
Post by: Phil Harvey on January 05, 2011, 04:05:17 PM
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
Title: Re: Thumbnail generation via perl API
Post by: jcsdoc on July 25, 2011, 10:53:28 AM
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
Title: Re: Thumbnail generation via perl API
Post by: Phil Harvey on July 25, 2011, 11:02:43 AM
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
Title: Re: Thumbnail generation via perl API
Post by: jcsdoc on July 26, 2011, 04:57:03 AM
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.
Title: Re: Thumbnail generation via perl API
Post by: Phil Harvey on July 26, 2011, 07:18:42 AM
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
Title: Re: Thumbnail generation via perl API
Post by: jcsdoc on July 26, 2011, 09:52:45 AM
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.