-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_iiif_img.pl
258 lines (209 loc) · 6.77 KB
/
create_iiif_img.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/perl
# nbt, 31.1.2018
# traverses image share and folder roots, creates (for each page)
# - info.json
# - .htaccess
# - thumbnail
use strict;
use warnings;
use lib './lib';
use Data::Dumper;
use Encode;
use HTML::Entities;
use HTML::Template;
use Image::Thumbnail;
use JSON;
use Path::Tiny;
use Readonly;
use ZBW::PM20x::Folder;
$Data::Dumper::Sortkeys = 1;
Readonly my $IIIF_ROOT_URI => 'https://pm20.zbw.eu/iiif/folder/';
Readonly my $IIIF_ROOT => path('/pm20/iiif/folder/');
Readonly my $IMAGEDATA_ROOT => path('/pm20/data/imagedata');
Readonly my @COLLECTIONS => qw/ co pe sh wa /;
Readonly my %RES_EXT => (
A => '_A.JPG',
B => '_B.JPG',
C => '_C.JPG',
);
my $info_tmpl =
HTML::Template->new( filename => 'html_tmpl/info.json.tmpl', );
my ( $imagedata_file, $imagesize_file, $imagedata_ref, $imagesize_ref, );
# check arguments
if ( scalar(@ARGV) == 1 ) {
if ( $ARGV[0] =~ m:^(co|pe|wa|sh)$: ) {
my $collection = $1;
mk_collection($collection);
} elsif ( $ARGV[0] =~ m:^(co|pe)/(\d{6}): ) {
my $collection = $1;
my $folder_nk = $2;
mk_folder( $collection, $folder_nk );
} elsif ( $ARGV[0] =~ m:^(sh|wa)/(\d{6},\d{6})$: ) {
my $collection = $1;
my $folder_nk = $2;
mk_folder( $collection, $folder_nk );
} elsif ( $ARGV[0] eq 'ALL' ) {
mk_all();
} else {
&usage;
}
} else {
&usage;
}
####################
sub mk_all {
foreach my $collection (@COLLECTIONS) {
mk_collection($collection);
}
}
sub mk_collection {
my $collection = shift or die "param missing";
# load input files
load_files($collection);
my $i = 0;
foreach my $folder_nk ( sort keys %{$imagedata_ref} ) {
$i++;
##next if ($i < 8100);
mk_folder( $collection, $folder_nk );
# debug and progress info
if ( $i % 100 == 0 ) {
print "$i folders done (up to $folder_nk)\n";
}
}
}
sub mk_folder {
my $collection = shift || die "param missing";
my $folder_nk = shift || die "param missing";
# open files if necessary
# (check with arbitrary entry)
if ( not defined $imagedata_ref ) {
print "loading imagedata\n";
load_files($collection);
}
my $folder = ZBW::PM20x::Folder->new( $collection, $folder_nk );
foreach my $doc_id ( keys %{ $imagedata_ref->{$folder_nk}{docs} } ) {
foreach my $page ( @{ $imagedata_ref->{$folder_nk}{docs}{$doc_id}{pg} } ) {
my $max_image_fn = get_max_image_fn( $folder_nk, $doc_id, $page );
my $image_id = substr( $page, 24, 4 );
## file name is 0 based; start page numbers with 1
my $page_no = sprintf( "%04d", $image_id + 1 );
my $image_uri =
get_image_uri( $collection, $folder_nk, $doc_id, $page_no );
my $image_dir =
get_image_dir( $collection, $folder_nk, $doc_id, $page_no );
my @rewrites;
# create iiif info
my %info_tmpl_var = ( image_uri => $image_uri, );
foreach my $res ( keys %RES_EXT ) {
my ( $width, $height ) = get_dim( $max_image_fn, $res );
unless ( length($width) and length($height) ) {
warn "width or height missing: $max_image_fn\n";
next;
}
my $real_url = get_image_real_url( $folder, $doc_id, $page, $res );
$info_tmpl_var{"width_$res"} = $width;
$info_tmpl_var{"height_$res"} = $height;
# add rewrite for .htaccess
push( @rewrites, { "max" => $real_url } ) if ( $res eq 'A' );
push( @rewrites, { "$width,$height" => $real_url } );
push( @rewrites, { "$width," => $real_url } );
}
$info_tmpl->param( \%info_tmpl_var );
write_info( $image_dir, $info_tmpl );
# make thumbnail
make_thumbnail( $max_image_fn, $image_dir )
unless -f "$image_dir/thumbnail.jpg";
# htaccess file
write_htaccess( $image_dir, \@rewrites );
}
}
}
sub load_files {
my $collection = shift || die "param missing";
$imagedata_file = $IMAGEDATA_ROOT->child("${collection}_image.json");
$imagedata_ref = decode_json( $imagedata_file->slurp );
$imagesize_file = $IMAGEDATA_ROOT->child("${collection}_size.json");
$imagesize_ref = decode_json( $imagesize_file->slurp );
}
sub get_max_image_fn {
my $folder_nk = shift || die "param missing";
my $doc_id = shift || die "param missing";
my $page = shift || die "param missing";
return
$imagedata_ref->{$folder_nk}{root} . '/'
. $imagedata_ref->{$folder_nk}{docs}{$doc_id}{rp}
. "/${page}_A.JPG";
}
sub get_image_dir {
my $collection = shift || die "param missing";
my $folder_nk = shift || die "param missing";
my $doc_id = shift || die "param missing";
my $page_no = shift || die "param missing";
my $image_dir =
$IIIF_ROOT->child($collection)->child($folder_nk)->child($doc_id)
->child($page_no);
$image_dir->mkpath;
return $image_dir;
}
sub get_image_uri {
my $collection = shift || die "param missing";
my $folder_nk = shift || die "param missing";
my $doc_id = shift || die "param missing";
my $page_no = shift || die "param missing";
return "$IIIF_ROOT_URI$collection/${folder_nk}/${doc_id}/${page_no}";
}
sub get_image_real_url {
my $folder = shift || die "param missing";
my $doc_id = shift || die "param missing";
my $page = shift || die "param missing";
my $res = shift || die "param missing";
# create url according to dir structure
my $url =
'/folder/'
. $folder->get_document_hashed_path($doc_id)->child('PIC')
->child( $page . $RES_EXT{$res} );
return $url;
}
sub get_dim {
my $max_image_fn = shift || die "param missing";
my $res = shift || die "param missing";
my $width = $imagesize_ref->{$max_image_fn}{$res}{w};
my $height = $imagesize_ref->{$max_image_fn}{$res}{h};
return ( $width, $height );
}
sub write_info {
my $image_dir = shift || die "param missing";
my $info_tmpl = shift || die "param missing";
my $json = $info_tmpl->output;
my $info_file = $image_dir->child('info.json');
$info_file->spew($json);
}
sub make_thumbnail {
my $max_image_fn = shift || die "param missing";
my $image_dir = shift || die "param missing";
# file is written
my $t = new Image::Thumbnail(
size => "150",
create => 1,
module => 'Image::Magick',
input => "$max_image_fn",
outputpath => "$image_dir/thumbnail.jpg",
);
}
sub write_htaccess {
my $image_dir = shift || die "param missing";
my $rewrites_ref = shift || die "param missing";
my $fh = $image_dir->child('.htaccess')->openw;
print $fh "RewriteEngine On\n";
foreach my $rewrite_ref ( @{$rewrites_ref} ) {
foreach my $from ( keys %{$rewrite_ref} ) {
my $to = $rewrite_ref->{$from};
print $fh "RewriteRule \"full/$from/0/default.jpg\" \"$to\" [PT]\n";
}
}
close($fh);
}
sub usage {
print "Usage: $0 {folder-id}|{collection}|ALL\n";
exit 1;
}