ExifTool Forum

ExifTool => The Image::ExifTool API => Topic started by: pfelipe on October 13, 2012, 02:54:48 AM

Title: Preview Raw Images - PreviewImage
Post by: pfelipe on October 13, 2012, 02:54:48 AM
Hi Phil,

I'm writing a perl/tk app and I would like to display a preview of raw files (Nikon NEF).

Is it practical to use PreviewImage for that? something like...

$nefpreviewWindow = $mw->Toplevel();

# got this from another post here:
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo($myFile,'JpgFromRaw','PreviewImage');
my $val = $$info{JpgFromRaw} || $$info{PreviewImage};
$val or die "No embedded JPG in $myFile\n";

$nefimagePreviewButton=$nefpreviewWindow->Button(
    -image=>$$val,
)->pack();

Or would I have to create a temporary file with the contents for $$val and then use this file as the Button image? My concern here is disk i/o could slow down the image preview...

Or could there be an easier method to show a preview of raw images?

Thank you,
Felipe.
Title: Re: Preview Raw Images - PreviewImage
Post by: Phil Harvey on October 13, 2012, 06:41:38 AM
Hi Felipe,

I'm sorry, I can't help you here.  I don't even know what graphics package you are using, and I have never done this myself.

Perhaps you will have more success asking in a forum with people that use this graphics package.

- Phil
Title: Re: Preview Raw Images - PreviewImage
Post by: pfelipe on October 15, 2012, 12:49:41 AM
This is what happens when you are too tired to troubleshoot... but insist to continue on... I should have learnt this lesson already...

8)

For reference, I got this working as follows:


# just a small extraction of my script... in case you wonder why there are no widgets on $mw...
use warnings;
use strict;

use Image::ExifTool;
use MIME::Base64;

$mw = MainWindow->new;

my $myFile=".\DSC_0001.NEF";

my $nefpreviewWindow = $mw->Toplevel();
$nefpreviewWindow->geometry("610x410");
my $exifTool = new Image::ExifTool;
# It seems no PreviewImage on Nikon NEF... using JpgFromRaw instead
my $info = $exifTool->ImageInfo($myFile,'JpgFromRaw');
my $val = $$info{JpgFromRaw} ;

my $nefmyImage=$nefpreviewWindow->Photo(
    'nef',
    -data=>encode_base64($$val)
);
my $nefsmallImage=$nefpreviewWindow->Photo('Preview');
my $imgFactor = &max(
    &factor( $nefmyImage->width, 600 ),
    &factor( $nefmyImage->height, 400 ),
);

$nefsmallImage->copy(
    $nefmyImage,
   -shrink,
   -subsample=> $imgFactor,$imgFactor
);


my $nefimagePreviewButton=$nefpreviewWindow->Button(
        -image=>$nefsmallImage,
)->pack();

sub factor {
    my( $n, $m ) = @_;
    ($n>$m) ? int($n/$m) : 1
}

sub max {
        my( $n, $m ) = @_;
        $n > $m ? $n : $m
}



Thank you Phill for a great tool!
:D
Title: Re: Preview Raw Images - PreviewImage
Post by: pfelipe on October 15, 2012, 01:11:14 AM
I was also able to use PerlMagick, but it is considerably slower to display the image preview (maybe it is the way I did it...)... also, the image looks darker when compared with the exifTool method above...

Anyway, here is how to do it with PerlMagick for those interested in displaying a preview of a raw file:

use MIME::Base64;
use Image::Magick;

$nefpreviewWindow = $mw->Toplevel();
$nefpreviewWindow->geometry("610x410");
      
$neffullImage=Image::Magick->new;
# read NEF file
$neffullImage->Read($myFile);
#convert to jpeg
$neffullImage->Set(magick=>'jpeg');
#scale image to preview size
$neffullImage->Scale(Geometry=>'600x400');

# store image in a Blob variable
my $nefmyImageData = $neffullImage->ImageToBlob() ;
      
$nefmyImage=$nefpreviewWindow->Photo(
    'Previewnef',
    #convert it to base64 to be used by widget
    -data=>encode_base64($nefmyImageData)
);
# memory cleaning
undef $nefmyImageData;      
# display preview image
$nefimagePreviewButton=$nefpreviewWindow->Button(
    -image=>$nefmyImage,
)->pack();

# memory cleaning
#undef $neffullImage;
@$neffullImage = ();

$nefpreviewWindow->configure(-title=>$myFile);
$nefimagePreviewButton->update;
select(undef, undef, undef, .1);


Regards,
Felipe.