in reply to Re: Tk show image from web
in thread Tk show image from web
Thank you. This was the hint I needed. Here a working script:
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'; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Tk show image from web (inspired app)
by Anonymous Monk on Nov 17, 2019 at 23:21 UTC | |
by Anonymous Monk on Dec 17, 2019 at 14:00 UTC | |
Re^3: Tk show image from web
by Anonymous Monk on Nov 16, 2019 at 23:42 UTC |