-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoalbum.pl
216 lines (183 loc) · 4.5 KB
/
photoalbum.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Switch 'Perl6';
use File::Find;
use PDF::Create;
use Image::ExifTool;
use Image::Magick;
my $im = Image::Magick->new;
sub proc_image {
my ($path, $short_name) = @_;
$image->Read($path);
$image->Set(quality => 300);
# at 300dpi, a 9cm image must be of at least 1.060 pixels
$image->AdaptiveResize(width => 1100, blur => -1);
my $name = './Album/' . $short_name;
$image->Write($name);
return $name;
}
sub get_images {
my @ret;
my $c = 0;
find({wanted => sub {
if (/\.jpg/) {
my $fname = proc_image($File::Find::name, $_);
push @ret, $fname if $fname;
}
$File::Find::prune = 1 if $c++ > 5;
}, follow => 1}, './SourceImages');
return @ret;
}
sub scale {
my ($w, $h, $default_width) = @_;
my $ratio = $h / $w;
return ($default_width, int($default_width * $ratio));
}
my $filename = 'album.pdf';
my $author = 'Leonardo Herrera';
my $title = '2009';
my $pdf = new PDF::Create(
'filename' => $filename,
'Version' => 1.2,
'PageMode' => 'UseOutlines',
'Author' => $author,
'Title' => $title,
'CreationDate' => [ localtime ],
);
my $psz = [0, 0, 8.25 * 75, 10.75 * 75 ];
my $root = $pdf->new_page('MediaBox' => $psz);
#my @images = (get_images)[0..40];
my @images = get_images;
my $page = $root->new_page;
my ($accum, $y) = (0, 0);
my ($pw, $ph) = (@$psz)[2,3];
sub decide_layout {
my $n = scalar @_;
given ($n) {
when 1 { return (1, 1); }
when 2 { return (1, 2); }
when 3 { return (1, 3); }
when 4 { return (2, 2); }
when 5 { return (2, 3); }
when 6 { return (2, 3); }
when 7 { return (2, 4); }
when 8 { return (2, 4); }
when 9 { return (3, 3); }
when 12 { return (3, 4); }
default { die "Wrong layout number: $_"; }
}
}
sub max {
my $max;
foreach (@_) { $max = $_ unless $max && $max > $_; }
return $max;
}
sub fit_img {
my ($img, $slot_w, $slot_h) = @_;
# aspect ratio
my $ratio = $$img{height} / $$img{width};
print "ratio: $ratio\n";
return ($slot_w, int($slot_h * $ratio));
}
sub place_img {
my ($page, $img, $w, $h, $slot_w, $slot_h, $x, $y) = @_;
my $margin = 10;
my $xscale;
if (abs($$img{width} - $w) > abs($$img{height} - $h)) {
$xscale = ($w - $margin * 2) / $$img{width};
} else {
$xscale = ($h - $margin * 2) / $$img{height};
}
my ($rw, $rh) = ($$img{width} * $xscale, $$img{height} * $xscale);
$x *= $slot_w;
$y *= $slot_h;
$x += ($slot_w - $rw) / 2;
$y += ($slot_h - $rh) / 2;
$page->image(image => $img,
xpos => $x,
ypos => $y,
xscale => $xscale,
yscale => $xscale,
);
}
sub layout {
my ($page, $pw, $ph, @images) = @_;
my $max_width = max map { $$_{width} } @images;
my $max_height = max map { $$_{height} } @images;
my ($w, $h) = decide_layout(@images);
my ($slot_w, $slot_h) = ($pw / $w, $ph / $h);
# margins
my $margin = 20;
for (my $j = 0; $j < $h; $j++) {
for (my $i = 0; $i < $w; $i++) {
my $im = shift @images;
if ($im) {
my ($w, $h) = fit_img($im, $slot_w, $slot_h);
place_img($page, $im,
$w, $h,
$slot_w, $slot_h,
$i, $j);
}
}
}
}
my @months = qw(Enero Febrero Marzo Abril
Mayo Junio Julio Agosto Septiembre
Octubre Noviembre Diciembre);
my $f = $pdf->font('Subtype' => 'Type1',
'Encoding' => 'WinAnsiEncoding',
'BaseFont' => 'Helvetica');
my $exif = new Image::ExifTool;
my %dt;
sub get_pics_for_month {
my ($m, $pics) = @_;
my @results;
my $x;
foreach my $k (keys %$pics) {
print "$m - $k... ";
unless ($dt{$k}) {
my $info = $exif->ImageInfo($k, 'DateTimeOriginal');
die "info err"
unless $info->{DateTimeOriginal} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d)/;
$dt{$k} = {
y => $1, m => $2, d => $3,
str => sprintf('%04d%02d%02d%02d%02d',
$1, $2, $3, $4, $5) };
}
if ($m == $dt{$k}->{m} - 1) {
print "matches.";
push @results, $k;
delete $pics->{$k};
}
print "\n";
}
return @results;
}
{
my $month = 0;
my %images = map { $_ => 1 } @images;
while (keys %images) {
die "pics without date info?" if $month > 11;
my @for_this_month =
sort { $dt{$a}->{str} cmp $dt{$b}->{str} }
get_pics_for_month($month, \%images);
if (@for_this_month) {
print "Pics for this month: ", scalar @for_this_month, "\n";
$page = $root->new_page;
$page->stringc($f, 40, 306, 426, $months[$month]);
while (@for_this_month) {
my $n = 6;
my @to_proc = reverse splice @for_this_month, 0, $n;
my @batch = map { $pdf->image($_) } @to_proc;
if (@batch) {
my $page = $root->new_page;
layout($page, $pw, $ph, @batch);
}
}
}
$month++;
}
}
$pdf->close;