-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-tiles.pl
executable file
·56 lines (41 loc) · 1.02 KB
/
make-tiles.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl -w
use Image::Magick;
use strict;
$| = 1;
if (@ARGV < 2) {
print "Usage: $0 tiles.png tile_size\nImages will be written to current directory.\n";
exit;
}
my $in = $ARGV[0];
my $size = $ARGV[1];
my $img = Image::Magick->new;
my $err = $img->Read($in);
die "$err" if $err;
my $w = $img->Get('width');
my $h = $img->Get('height');
if ($w % $size != 0) {
die "Width of image ($w) is not a multiple of $size";
}
if ($h % $size != 0) {
die "Height of image ($h) is not a multiple of $size";
}
my $tiles_across = $w / $size;
my $tiles_down = $h / $size;
print "Making ${size}x${size} tiles, $tiles_across across and $tiles_down down";
my $c = 0;
my ($i, $j);
for ($j = 0; $j < $tiles_down; $j++) {
for ($i = 0; $i < $tiles_across; $i++) {
my $img2 = $img->Clone();
$img2->Crop(width => $size,
height => $size,
x => ($i * $size),
y => ($j * $size));
my $err = $img2->Write("tmp.png");
`pngcrush tmp.png $c.png`;
unlink("tmp.png");
print ".";
$c++;
}
}
print "done\n";