#!/usr/bin/perl -w use strict; use LWP::UserAgent; use Image::Magick; use Getopt::Std; getopts('h:w:o:m:s:'); our($opt_h, $opt_w, $opt_o, $opt_m, $opt_s); my $log_file = "/tmp/mergelog"; my $output_file = $opt_o || die "Please specify an output file with -o\n"; my $width_out = $opt_w || 600; my $height_out = $opt_h || 400; my $max_img = $opt_m || 20; my $sleep_time = $opt_s || 30; my $running = 1; open(LOG,">>$log_file") || warn $!; my $img = Image::Magick->new; $img->Set(size=>"$width_out"."x$height_out"); while ($running) { # get a fresh batch of random pictures my $ua = new LWP::UserAgent; $ua->agent("Mozilla/4.5 "); $ua->timeout(30); my $url = getGoogleURL(); my $response = $ua->get($url); my $content = $response->content; my @imgurls = ($content =~ m|imgurl=([^&]+)\&|gis); for my $imgurl (@imgurls) { my $tmpimg = Image::Magick->new; my $response = $ua->get($imgurl); next unless ($response->is_success); print LOG time()."\t$imgurl\n"; # grab the image and suck it in my $tmp_file = "/tmp/$$".time().".jpg"; open(IMG,">$tmp_file") || die "can't open $tmp_file: $!"; print IMG $response->content; close(IMG); if (`/usr/bin/file -b $tmp_file` =~ /jpe?g/i) { $tmpimg->Read(filename=>$tmp_file); $tmpimg->Resize(width=>$width_out, height=>$height_out); unlink($tmp_file); } else { unlink($tmp_file); next; } # put the image on the list push @$img, @$tmpimg; shift @$img if (@$img > $max_img); # mash all the images together if (@$img > 1) { my $cimg = $img->Average(); $cimg->Resize(width=>$width_out, height=>$height_out); my $tmp_file = "/tmp/$$".time()."tmp.jpg"; $cimg->WriteImage($tmp_file); rename($tmp_file, $output_file); } sleep $sleep_time; } } sub getGoogleURL { # adapted from Dave Mattson's url(http://diddly.com/random/) my $googleurl = "http://images.google.com/images?q="; my @mon = (0..9,'a','b','c'); my @camtype = ("dcp0", "dsc0", "dscn", "mvc-", "mvc0", "P101", "P", "IMG_", "imag", "1", "dscf", "pdrm", "IM00", "EX00", "dc", "pict", "P00", "", "", "imgp", "pana", "1"); my @ranges = (4000, 4000, 4000, 400, 500, 4000, 50, 4000, 130, 100, 4000, 600, 850, 100, 4000, 600, 12000, 30, 50, 2000, 200, 100); my @widths = (4, 4, 4, 3, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 5, 4, 3, 4, 4, 2); my $choice = int(rand @camtype); my $str = $camtype[$choice]; my $range = $ranges[$choice]; my $width = $widths[$choice]; if ($choice == 6) { $str .= $mon[rand @mon]; $str .= sprintf("%02d", int rand 31); } elsif ($choice == 9) { my $strt = sprintf("%02d", int(rand 3)); $str .= $strt.'-'.$strt; } elsif ($choice == 17) { $str .= sprintf("%02d",int(rand 13)); $str .= sprintf("%02d",int(rand 31)); } elsif ($choice == 18) { $str .= sprintf("%02d", int(rand 3)); $str .= $mon[rand @mon]; $str .= sprintf("%02d", int rand 31); } $str .= sprintf("%0".$width."d", int(rand $range)); if ($choice == 14) { my @sz = ('s','m','l'); $str = $camtype[$choice] . sprintf("%04d", int(rand 190)) . $sz[rand @sz]; } elsif ($choice == 21) { my $strt = sprintf("%02d", int(rand 90)); $str = "1$strt\-$strt" . sprintf("%0".$width."d", int(rand $range)) . "_IMG"; } return "$googleurl$str.jpg&num=20&btnG=Google+Search&as_filetype=jpg&imgsafe=off"; } close(LOG);