I needed a large number of national flag images at a certain resolution for a project my Web Development course was working on. By examining some flag images I saw on Wikipedia, I noticed that they were creating flag images on the fly.
For example, to create the United Kingdom's flag that is 100 pixels wide, you can access this URL:
http://upload.wikimedia.org/wikipedia/commons/thumb/4/45/Flag_of_the_United_Kingdom.svg/100px-the_United_Kingdom.pngwhich produces this flag:

So I developed a Perl script that would automate this process for me. I've included it below for anyone else who might need flag images. Note that I had to set the user agent string, or Wikipedia would not respond properly to the http request. If you use this script to download a lot of images, please be nice throttle your requests with the sleep() command.
15 | my $flag = 'the United Kingdom' ; |
17 | my $filename = lc $flag ; |
19 | $filename = $filename . "_" . $image_size . ".png" ; |
21 | my $url_filename = $flag ; |
22 | $url_filename =~ s/\s/_/g; |
25 | $url_filename . ".svg/" . $image_size . "px-" . $url_filename . ".png" ; |
27 | print "Getting $img_url\n" ; |
29 | my $ua = LWP::UserAgent->new; |
30 | $ua ->agent( 'Mozilla/5.0 Firefox 5.6' ); |
31 | $ua ->from( 'your@email.com' ); |
33 | my $response = $ua ->get( $img_url ); |
35 | if ( $response ->is_success) { |
36 | print "Writing to $filename\n" ; |
38 | open (IMG, ">$filename" ); |
40 | print IMG $response ->content; |
44 | print "ERROR: Could not download.\n" ; |
No comments:
Post a Comment