source: trunk/npemap.org.uk/scripts/tile-processing/makeThumbImage.pl @ 511

Last change on this file since 511 was 511, checked in by Nick Burch, 15 years ago

Include Shetland on allmap page

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/perl
2# Generate the "whole of the UK" thumbnail from lots of files in scaled50
3
4use List::Util qw[min max];
5use Image::Magick;
6use strict;
7
8my $tilesize = 125;
9
10my $inscale = 50;
11my $outscale = 125;
12
13# Find these values by hand using the gimp or similar
14#  lower easting,northing
15my @bl_trims = qw( 77, 0 );
16#  upper easting,northing
17my @tr_trims = qw( 657, 1057 );
18
19# which files to build it from
20my @files = glob("scaled${inscale}/???/???.jpg");
21my %tiles ;
22
23my ($minx, $miny, $maxx, $maxy);
24
25foreach my $f (@files) {
26  $f =~ m/\/(\d+)\/(\d+)\./;
27  my ($x, $y) = ($1 +0, $2 +0);
28  $tiles{$x} ||= {};
29  $tiles{$x}{$y} = $f;
30}
31
32# Ensure it starts at 0,0
33# (Otherwise, deciding where you clicked on is too hard)
34$maxx = max(keys %tiles) + 1;
35$maxy = max(map { max(keys %{$tiles{$_}})} keys %tiles) + 1;
36
37# Decide how big our intermediate, and final images will be
38my $temp_x = $maxx * $tilesize;
39my $temp_y = $maxy * $tilesize;
40
41my $out_x = int($maxx * $tilesize * $inscale / $outscale); 
42my $out_y = int($maxy * $tilesize * $inscale / $outscale);
43
44my $trim_width = $tr_trims[0] - $bl_trims[0];
45my $trim_height = $tr_trims[1] - $bl_trims[1];
46
47print "There are $maxx images across, and $maxy images down\n";
48print "The temp image will be $temp_x x $temp_y\n";
49print "The joined image will be $out_x x $out_y\n";
50print "The final image will be $trim_width x $trim_height\n\n";
51
52
53# Render the larger image, where we just include all the scaled images
54my $out = Image::Magick->new(size => $temp_x."x".$temp_y);
55$out ->ReadImage('xc:white');
56
57foreach my $x (0..($maxx)) {
58        foreach my $y (0..($maxy)) {
59                my $input = $tiles{$x}{$y};
60                if(-f $input) {
61                        print "Including $input\n";
62                        my $image = Image::Magick->new;
63            print $image->Read($input);
64
65                        # x goes from 0 to n
66                        my $this_x = $x * $tilesize;
67                        # y goes from 0 to n
68                        my $this_y = $temp_y - (($y+1) * $tilesize);
69                        print "   goes at $this_x , $this_y\n";
70
71            print $out->Composite(image=>$image, x=>$this_x, y=>$this_y);
72            undef $image;
73        }
74     }
75}
76
77# Now resize it
78$out->Resize("${out_x}x${out_y}");
79# And make it a bit darker, so it's easier to see
80$out->Tint(fill=>"white", opacity=>65);
81
82# Write this out as a temp file
83$out->Set(quality=>100);
84$out->Write("jpg:/tmp/thumbnail.jpg");
85
86
87# Produce the large image - crop left, right and bottom
88$out->Crop( x=>$bl_trims[0], y=>0,
89            width=>($tr_trims[0] - $bl_trims[0]),
90            height=>$out_y );
91$out->Set(quality=>85);
92$out->Write("jpg:thumbnail.jpg");
93print "\nOutput thumbnail.jpg at ${trim_width}x${out_y}\n";
94
95
96# Now do the smaller image - crop all 4
97$out = Image::Magick->new;
98$out->Read("/tmp/thumbnail.jpg");
99
100# And trim the edges off it
101$out->Crop( x=>$bl_trims[0], y=>($out_y - $tr_trims[1]),
102            width=>($tr_trims[0] - $bl_trims[0]),
103            height=>($tr_trims[1] - $bl_trims[1]) );
104
105# And do half size
106($trim_width,$trim_height) = (int($trim_width/2),int($trim_height/2));
107$out->Resize("${trim_width}x${trim_height}");
108
109# Finally, write it out
110$out->Set(quality=>85);
111$out->Write("jpg:thumbnailsmall.jpg");
112undef $out;
113
114print "\nOutput thumbnailsmall.jpg at ${trim_width}x${trim_height}\n";
115print "($maxx,$maxy)\n";
Note: See TracBrowser for help on using the repository browser.