source: subversion/applications/utils/tagwatch/getPhotos.pl @ 7084

Last change on this file since 7084 was 6725, checked in by hakan, 12 years ago

Etric Celine asked on osm-talk to commit his changes

File size: 2.4 KB
Line 
1#-----------------------------------------------------------------
2# Downloads photos showing a real-life example of each OpenStreetmap
3# tag
4#-----------------------------------------------------------------
5# Usage: perl getPhotos.pl
6# Will create an ./html/photos directory and fill it with JPEG images
7# Uses input from http://wiki.openstreetmap.org/index.php/Tagwatch/Photos
8#-----------------------------------------------------------------
9# This file is part of Tagwatch
10# Tagwatch is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Tagwatch is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Tagwatch.  If not, see <http://www.gnu.org/licenses/>.
22#---------------------------------------------------------------
23use strict;
24use MediaWiki;
25use LWP::Simple;
26use GD;
27
28my $c = MediaWiki->new;
29
30$c->setup({'wiki' => {
31            'host' => 'wiki.openstreetmap.org',
32            'path' => ''}});
33
34my $PDir = "html/photos";
35my $CacheDir = "original_photos";
36mkdir "html" if ! -d "html";
37mkdir $PDir if ! -d $PDir;
38mkdir $CacheDir if ! -d $CacheDir;
39
40foreach my $Line(split(/\n/, $c->text("Tagwatch/Photos"))){
41 
42  if($Line =~ m{^\*\s*(\w+)=(\w+)\s+(.*?)\s*$}){
43    print "Fetching: $1 = $2\n";
44   
45    # Download the image
46    my $Filename = "$PDir/$1_$2.jpg";
47    my $URL = $3;
48   
49    my $CacheName = $URL;
50    $CacheName =~ s/\W/_/g;
51    $CacheName = "$CacheDir/$CacheName.jpg";
52    mirror($URL, $CacheName);
53   
54    my $Data;
55    {
56    local($/);
57    open(LOAD, $CacheName) || die;
58    binmode LOAD;
59    $Data = <LOAD>;
60    close LOAD;
61    }
62   
63    # Open it in GD
64    my $Image1 = GD::Image->newFromJpegData($Data);
65
66    # Calculate image size
67    my $WO = $Image1->width;
68    my $W = 200;
69    my $Ratio = $W / $WO;
70    my $HO = $Image1->height;
71    my $H = $HO * $Ratio;
72   
73    # Make a resized copy, and save that
74    my $Image2 = new GD::Image($W,$H);
75    $Image2->copyResampled($Image1,0,0,0,0,$W,$H,$WO,$HO);
76    open(IMOUT, ">$Filename") || die;
77    binmode IMOUT;
78    print IMOUT $Image2->jpeg();
79    close IMOUT;
80
81  }
82}
Note: See TracBrowser for help on using the repository browser.