[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...embed Images into an standalone perl/Tk executable...

<< Back to: comp.lang.perl.tk FAQ part0 of 5

Question by Steve
Submitted on 8/11/2003
Related FAQ: comp.lang.perl.tk FAQ part0 of 5
Rating: Rate this question: Vote
How can I embed Images into an standalone perl/Tk executable?
Right now the executable calls up images from a server at run time (very inefficient)
I would like to be able to compile the images
as part of the executable


Answer by Pete Barnett
Submitted on 8/20/2003
Rating:  Rate this answer: Vote
Use the data attribute of the images to get the base64 encoded image data, like this sample program which encodes all the images in a named subdirectory:-

<code>
#!/usr/bin/perl -w

use MIME::Base64;
use Tk;
use Tk::Photo;

my %formats =
(
bmp => 'bmp',
gif => 'gif',
jpg => 'jpeg',
ppm => 'ppm',
);


my($DH, $FH, $OUT, $filename, $data, $mw);
$mw = MainWindow->new();
local $/ = undef;
defined($ARGV[0]) or die "Usage: tkembed.pl directory_name\n";
opendir(DH,$ARGV[0]) or die "Can't open $ARGV[0]; $!\n";
open(OUT,">test.dat")or die "Can't open test.dat; $!\n";

while(defined($filename = readdir(DH)))
{
    next if($filename =~ /^\./);
    print "Processing $filename...\n";
    $filename =~ m/\.(\w+)$/;
    my $img = $mw->Photo(
                         -format => $formats{$1},
                         -file   => "$ARGV[0]/$filename",
                        );
    
    print OUT $filename,"|\n",$img->data(-format=>$formats{$1}),"|\n";
}
closedir(DH);
close(OUT);
</code>

You can then append the output file created by this code to your program after an __END__ directive. Having done that, you can read the data using the <DATA> filehandle; I've done it by building a hash table for the data with the filenames as keys, like this:-

<code>
sub loadfiles
{
    local $/ = "|\n"; # 'Cause <DATA> text is delimited by this non-Base64 char
    
    my($filename, $data);
    
    for(;;)
    {
        $filename = <DATA>;
        last unless defined($filename);
        $filename =~ s/\|\n$//;
        $data = <DATA>;
        $data =~ s/\|\n$//;
        $filedata{$filename} = $data;
    }
}    
</code>

Now create your images like this :-

<code>
my %formats =
(
bmp => 'bmp',
gif => 'gif',
jpg => 'jpeg',
);
    $_ =~ m/\.(\w+)$/;
    my $fmt = $formats{$1};
    my $img = $mw->Photo(
                         -format => $fmt,
                         -data   => $filedata{$_},
                        );
</code>

where $_ is the filename (I'm getting the format from the file extension; you could put the name and the format in as literals if you'd rather.

Hope this helps!

Pete.

 

Answer by Pete Barnett
Submitted on 8/20/2003
Rating: Not yet rated Rate this answer: Vote
Note that the site has managed to strip out all the backslashes from the code; notably removing the backslash from all the carriage-return escape sequences, and the backslash to escape the . metacharacter where it's extracting the extension from the filename :-(

Pete.

 

Answer by Michael Carey
Submitted on 11/2/2003
Rating:  Rate this answer: Vote
Try this, let me know what you think.

http://www.giantsquidmarks.com/bintoscalar.html

 

Answer by MEGSTERTHEDOGLOVER
Submitted on 7/24/2005
Rating: Not yet rated Rate this answer: Vote
i have a question about my dog i whine and my dog lily going INSANE!!!!!! is it a sign of giving birth to puppies or has already had them?



               THANKS!
                        Sincerely,
                                 MEGSTERTHEDOGLOVER

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.perl.tk FAQ part0 of 5


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.