Reputation: 2833
I'm using this code to save images from a specific URL using Image::Grab. But the image gets saved in 0 bytes so there is no image.
#!/usr/bin/perl
use Image::Grab;
$pic->url('hhttp://www.vikingskipet.no/cam_1.jpg')
$pic->grab;
# Now to save the image to disk
$time = time();
open(IMAGE, ">$time.jpg") || die"$time.jpg: $!";
binmode IMAGE; # for MSDOS derivations.
print IMAGE $pic->image;
close IMAGE;
I hope someone can help me. I'm using cronjobs to run the script every minute.
Upvotes: 0
Views: 3455
Reputation: 126722
The main problem is that you haven't created an Image::Grab
object. You should certainly use strict
and use warnings
at the start of all your programs, especially when you are asking for help with them. That will allow Perl to find and report simple mistakes that you may have overlooked.
This program does what you intended, as well as putting the file name into a more readable form
use strict;
use warnings;
use Image::Grab;
my $pic = Image::Grab->new;
$pic->url('http://www.vikingskipet.no/cam_1.jpg');
$pic->grab;
my $file = do {
my @dt = reverse((localtime)[0..5]);
$dt[0] += 1900;
$dt[1]++;
sprintf "%04d-%02d-%02d.%02d.%02d.%02d.jpg", @dt;
};
open my $img, '>:raw', $file or die qq(Can't open "$file" for output: $!);
print $img $pic->image;
close $img or die qq(Failed to close "$file": $!);
print qq(Image file "$file" saved successfully\n);
Upvotes: 6