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

Last change on this file since 421 was 421, checked in by Nick Burch, 13 years ago

Do the cropping at the same time

  • Property svn:executable set to *
File size: 2.5 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, 877 );
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 trim the edges off it
80$out->Crop( x=>$bl_trims[0], y=>($out_y - $tr_trims[1]),
81            width=>($tr_trims[0] - $bl_trims[0]),
82            height=>($tr_trims[1] - $bl_trims[1]) );
83# And make it a bit darker, so it's easier to see
84$out->Tint(fill=>"white", opacity=>65);
85
86# Finally, write it out
87$out->Set(quality=>85);
88$out->Write("jpg:thumbnail.jpg");
89undef $out;
90print "\nOutput at ${trim_width}x${trim_height}\n";
91print "($maxx,$maxy)\n";
Note: See TracBrowser for help on using the repository browser.