IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks

It is easy to display an image into a Tk widget, for instance, a Label or a Text widget. All examples I have seen refer to image files stored locally. I want to show images available on a server for which I have a URL. Is there an alternative to downloading the file image locally and then show it in Tk? A little bit like a Browser (which I think does not save all images locally to display them).

If there is no alternative than downloading the file locally, I try to to write the image to a temporary file:

use LWP::Simple; use File::Temp; my $tempFile = new File::Temp( UNLINK => 0, SUFFIX => '.jpg' ); #my $tempFile = 'img.jpg'; my $url='https://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/Win +d_turbines_in_southern_California_2016.jpg/200px-Wind_turbines_in_sou +thern_California_2016.jpg' getstore($url, $tempFile);

Unfortunately my file is empty, and I do not get any error message! If I write it to an ordinary directory, however, I can download the file fine. Any idea?

Replies are listed 'Best First'.
Re: Tk show image from web
by Discipulus (Canon) on Nov 16, 2019 at 17:32 UTC
    Hello IB2017,

    you dont need to download the image into a file: you can use LWP to grab the image and store its content inside a variable(you might need to transform it in base64) and use the -data option while building the Tk::Photo object.

    -data => string Specifies the contents of the image as a string. The string can co +ntain base64 encoded data or binary data. The format of the string mu +st be one of those for which there is an image file format handler th +at will accept string data. If both the -data and -file options are s +pecified, the -file option takes precedence.

    L*

    PS I said TITS but you took it literally (see below: the original code posted and now changed, used breast as search word ;)

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thank you. This was the hint I needed. Here a working script:

      • for a given term, it searches an image in Wikipedia
      • if something is available, it grabs it and prints it in Tk

      Maybe somebody can find some inspiration

      use strict; use warnings; use HTTP::Request; use LWP::UserAgent; use JSON::PP; use LWP::Simple; use Tk; use Tk::Photo; use Tk::JPEG; use Tk::PNG; use MIME::Base64; my $ua = LWP::UserAgent->new; my $QueryString="wind park"; my $urlImage=getWikiImage($QueryString); if ($urlImage){ my $data=grabbingImage($urlImage); $data = MIME::Base64::encode_base64($data); #creating GUI my $mw = new MainWindow(); my $top = $mw->Frame()->pack(); my $img = $mw->Photo(-data => $data); my $label = $top->Label(-image => $img)->pack(); MainLoop; } sub getWikiImage{ my $QueryString=shift; print "Querying Wiki for $QueryString\n"; my $imgUrl; my $url="https://en.wikipedia.org/w/api.php?action=query&title +s=" . $QueryString . "&prop=pageimages&format=json&pithumbsize=200"; my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); if ($res->is_success) { my $json_text= $res->content; my $decoded_json = decode_json( $json_text ); my @WikiItem = $decoded_json->{'query'}{'pages'}; foreach my $hash_ref (@WikiItem) { foreach (keys %{$hash_ref}) { $imgUrl = ${$hash_ref}{$_}{'thumbnail'}{'source'}; } } } else { print "Error\n"; } print "IMG: $imgUrl \n"; return $imgUrl; } sub grabbingImage{ my $url=shift; print "Grabbing image...\n"; my $request = new HTTP::Request 'GET', $url; my $response = $ua->request($request); if ($response->is_success) { return $response->content; } else { print $response->error_as_HTML; return 'error'; } }
        Maybe somebody can find some inspiration

        Very cool, thank you!

        #!/usr/bin/perl =head1 DESCRIPTION View random images from the supplied folder (supports . and ~). Every time you close an image a new random image will open, until the program is stopped. Inspired by "Re^2: Tk show image from web" by IB2017 https://www.perlmonks.org/index.pl?node_id=11108799 =cut use strict; use warnings; use autodie; use File::Spec; use MIME::Base64; use Tk; use Tk::Photo; use Tk::JPEG; use Tk::PNG; my $dir = shift || die "Usage: $0 /path/to/images"; die 'Folder not found :-(' unless -d $dir; opendir my $dh, glob $dir; my @img = grep /\.(jpe?g|gif|png|bmp)/i, readdir $dh; closedir $dh; die 'No images found, try again!' unless @img; while () { my $img = $img[rand@img]; my $path = File::Spec->catfile($dir,$img); open my $fh, '<', $path; my $dat = join '', <$fh>; close $fh; $dat = MIME::Base64::encode_base64($dat); print "$path \n"; my $mw = MainWindow->new; my $top = $mw->Frame()->pack(); my $image = $mw->Photo(-data => $dat); my $label = $top->Label(-image => $image)->pack(); MainLoop }
        The wikipedia lookup doesn't work for me so I changed 1 line to make it load any image or a default image:
        my $urlImage= getWikiImage($QueryString) || shift || 'https://covers.oreillystatic.com/images/9780596000271/lrg.jpg';