Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl grabbing an external image

Reply
Thread Tools

Perl grabbing an external image

 
 
kjhjhjhjadsasda@urbanhabit.com
Guest
Posts: n/a
 
      12-15-2005
Hi

Im trying to grab an external image (through url) and save it locally
on the server. Im using image::grab like so:


use Image::Grab;
$pic = new Image::Grab;
$pic->url('http://groups.google.com/groups/img/groups_medium.gif');
$pic->grab;

open(IMAGE, ">$serverpath");
binmode IMAGE;
print IMAGE $pic->image;
close IMAGE;

This saves a file on the $serverpath location but it keeps being
nothing eg 0k. I remember Ive ran into this previously some time.

Any suggestions??..

Thanks!
M

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-16-2005
wrote:

> Hi
>
> Im trying to grab an external image (through url) and save it locally
> on the server. Im using image::grab like so:
>
>
> use Image::Grab;


use strict;
use warnings;


> $pic = new Image::Grab;
> $pic->url('http://groups.google.com/groups/img/groups_medium.gif');
> $pic->grab;
>
> open(IMAGE, ">$serverpath");


check if this succeeds

> Any suggestions??..


See above.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      12-16-2005
John Bokma wrote:
> use strict;
> use warnings;
> ...
> check if this succeeds


FWIW, I've never used Image::Grab, but I did give a quick try to the
OP's code (but within the ordinary BP guidelines as John suggested).
The $pic object is never populated with data by the grab method. I get
a warning on the print (because it's printing null), but otherwise the
code does exactly what the OP says (ie, not much).

The OP's code is pretty much straight out of the perldocs for the
module (which are crap, BTW - many examples contain syntax or usage
errors).

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      12-16-2005

<>
>
> The OP's code is pretty much straight out of the perldocs for the
> module (which are crap, BTW - many examples contain syntax or usage
> errors).
>


In that case, I suggest (untested) :

use strict;
use warnings;
use LWP::Simple;
my $url ='http://groups.google.com/groups/img/groups_medium.gif';

my $content = get($url);
open(IMAGE, ">filename.gif") or die "$!";
binmode IMAGE;
print IMAGE $content;
close IMAGE or die "$!";

__END__

Cheers,
Rob


 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      12-16-2005
"Sisyphus" <> wrote:

>
> <>
>>
>> The OP's code is pretty much straight out of the perldocs for the
>> module (which are crap, BTW - many examples contain syntax or usage
>> errors).
>>

>
> In that case, I suggest (untested) :
>
> use strict;
> use warnings;
> use LWP::Simple;
> my $url ='http://groups.google.com/groups/img/groups_medium.gif';
>
> my $content = get($url);
> open(IMAGE, ">filename.gif") or die "$!";
> binmode IMAGE;
> print IMAGE $content;
> close IMAGE or die "$!";


If you don't use LWP::Simple, you can simply (ha ha) drop the file stuff:

use strict;
use warnings;

use LWP::UserAgent;

my $url = 'http://groups.google.com/groups/img/groups_medium.gif';

my $response = LWP::UserAgent->new->get(

$url,
':content_file' => 'filename.gif'
);

$response->is_success or
die "Download failed: ", $response->status_line, "\n";

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-16-2005
<> wrote:


> an external image



What would an "internal image" be then?


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      12-16-2005
Tad McClellan <> wrote:

> <> wrote:
>
>
>> an external image

>
> What would an "internal image" be then?


The thing after the successfull grabbing

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      12-16-2005
wrote:
> Hi
>
> Im trying to grab an external image (through url) and save it locally
> on the server. Im using image::grab like so:
>
>
> use Image::Grab;
> $pic = new Image::Grab;
> $pic->url('http://groups.google.com/groups/img/groups_medium.gif');
> $pic->grab;
>
> open(IMAGE, ">$serverpath");
> binmode IMAGE;
> print IMAGE $pic->image;
> close IMAGE;
>
> This saves a file on the $serverpath location but it keeps being
> nothing eg 0k. I remember Ive ran into this previously some time.
>
> Any suggestions??..




I don't know the module so I'll assume your code is ok.

The obvious problem to me is that google block certain useragents, I
don't know what useragent the module declares itself to be, but it's
probably blocked, as is LWP::Simple (libwww-perl/x.xxx).

You'll need to set the useragent to something a browser would use.


$ perl -MLWP::Simple -e 'print length
get("http://groups.google.com/groups/img/groups_medium.gif")'

0

$ perl -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new( agent =>
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; CS.v0.2; .NET CLR
1.0.3705)" );my
$res=$ua->get("http://groups.google.com/groups/img/groups_medium.gif");print
length $res->content;'

5183




--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grabbing GPS data using perl deadpickle Perl Misc 0 03-07-2008 09:48 PM
grabbing clipboard image jobs Javascript 0 05-15-2007 02:38 PM
grabbing a traditional form value via .net (instead of referencing an object) darrel ASP .Net 4 07-07-2004 03:06 PM
Re: Grabbing All Cookies S. Justin Gengo ASP .Net 0 07-29-2003 03:05 AM
Re: Grabbing All Cookies Ken Cox [Microsoft MVP] ASP .Net 0 07-29-2003 02:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57