blib/lib/Panotools/Photos.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 45 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 286 SCAR 69.6
CC
32
Cov
0%
Layered
56.0
Rotation
56.0
Bracketed
42.0
CRAP 1056.0
1 package Panotools::Photos;
2  
3 =head1 NAME
4  
5 Panotools::Photos - Photo sets
6  
7 =head1 SYNOPSIS
8  
9 Query sets of photos
10  
11 =head1 DESCRIPTION
12  
13 A collection of photos has possibilities, it could be one or more panoramas or a
14 bracketed set. This module provides some methods for describing groups of
15 photos based on available metadata
16  
17 =cut
18  
19 use strict;
20 use warnings;
21  
22 use File::Spec;
23 use Image::ExifTool;
24  
25 =head1 USAGE
26  
27 Create a new object like so:
28  
29 my $photos = new Panotools::Photos;
30  
31 Alternatively supply some filenames:
32  
33 my $photos = new Panotools::Photos ('DSC_0001.JPG', 'DSC_0002.JPG');
34  
35 =cut
36  
37 sub new
38 {
39 my $class = shift;
40 $class = ref $class || $class;
41 my $self = bless [], $class;
42 $self->Paths (@_);
43 return $self;
44 }
45  
46 =pod
47  
48 Add to or get the list of image filenames:
49  
50 $photos->Paths ('DSC_0003.JPG', 'DSC_0004.JPG');
51 my @paths = $photos->Paths;
52  
53 =cut
54  
55 sub Paths
56 {
57 my $self = shift;
58 for my $path (@_)
59 {
60 push @{$self}, {path => $path, exif => Image::ExifTool::ImageInfo ($path)};
61 }
62 return map ($_->{path}, @{$self});
63 }
64  
65 =pod
66  
67 Construct a stub filename from the names of the first and last images in the
68 list.
69  
70 my $stub = $photos->Stub;
71  
72 e.g. DSC_0001.JPG DSC_0002.JPG DSC_0003.JPG -> DSC_0001-DSC_0003
73  
74 =cut
75  
76 sub Stub
77 {
78 my $self = shift;
79 my $path_a = $self->[0]->{path};
80 my $path_b = $self->[-1]->{path};
81 # strip any suffixes
82 $path_a =~ s/\.[[:alnum:]]+$//;
83 $path_b =~ s/\.[[:alnum:]]+$//;
84 # strip all but filename
85 $path_b =~ s/.*[\/\\]//;
86 return $path_a .'-'. $path_b;
87 }
88  
89 =pod
90  
91 Query to discover if this is a likely bracketed set. i.e. is the total number
92 of photos divisible by the number of different exposures:
93  
94 &do_stuff if ($photos->Bracketed);
95  
96 =cut
97  
98 sub Bracketed
99 {
100 my $self = shift;
101 # bracketed photos are not shot on 'Auto Exposure'
102 for my $index (0 .. scalar @{$self} -1)
103 {
104 next unless defined $self->[$index]->{exif}->{ExposureMode};
105 return 0 if $self->[$index]->{exif}->{ExposureMode} eq 'Auto';
106 }
107 my $brackets = scalar (@{$self->Speeds});
108 # require equal numbers of each exposure level
109 return 0 if (scalar (@{$self}) % $brackets);
110 # require more than one exposure level
111 return 0 if ($brackets < 2);
112 # require bracketing order repeats: 1/50 1/100 1/200 1/50 1/100 1/200 etc...
113 for my $index ($brackets .. scalar @{$self} -1)
114 {
115 return 0 unless $self->[$index]->{exif}->{ExposureTime} eq $self->[$index - $brackets]->{exif}->{ExposureTime};
116 }
117  
118 return 1;
119 }
120  
121 =pod
122  
123 Query to discover if this is a layered set, i.e. there is a large exposure
124 difference in the set, but it isn't bracketed.
125  
126 &do_stuff if ($photos->Layered);
127  
128 By default the threshold is 4, e.g. exposures varying between 2 and 1/2
129 seconds indicate layers. Vary this threshold like so:
130  
131 &do_stuff if ($photos->Layered (2));
132  
133 =cut
134  
135 sub Layered
136 {
137 my $self = shift;
138 my $factor = shift || 4;
139 return 0 if $self->Bracketed;
140 my $longest = $self->Speeds->[0];
141 my $shortest = $self->Speeds->[-1];
142 if ($longest =~ /^1\/([0-9]+)$/) {$longest = 1 / $1};
143 if ($shortest =~ /^1\/([0-9]+)$/) {$shortest = 1 / $1};
144 return 0 unless $longest or $shortest;
145 return 0 if $shortest == 0;
146 return 0 if $longest / $shortest < $factor;
147 return 1;
148 }
149  
150 =pod
151  
152 Get a list of exposure times sorted with longest exposure first
153  
154 @speeds = @{$photos->Speeds};
155  
156 =cut
157  
158 sub Speeds
159 {
160 my $self = shift;
161 my $speeds = {};
162 for my $image (@{$self})
163 {
164 my $et = $image->{exif}->{ShutterSpeedValue} || $image->{exif}->{ExposureTime} || $image->{exif}->{ShutterSpeed} || 0;
165 $speeds->{$et} = 'TRUE';
166 }
167 return [sort {_normalise ($b) <=> _normalise ($a)} keys %{$speeds}];
168 }
169  
170 sub _normalise
171 {
172 my $number = shift;
173 if ($number =~ /^1\/([0-9]+)$/) {$number = 1 / $1};
174 return $number;
175 }
176  
177 =pod
178  
179 Given a set of photos, split it into a one or more sets by looking at the
180 variation of time interval between shots. e.g. typically the interval between
181 shots in a panorama varies by less than 15 seconds. A variation greater than
182 that indicates the start of the next panorama:
183  
184 my @sets = $photos->SplitInterval (15);
185  
186 Sets with an average interval greater than 4x this variation are not considered
187 panoramas at all and discarded.
188  
189 =cut
190  
191 sub SplitInterval
192 {
193 my $self = shift;
194 my $d_inc = shift || 15;
195 my $max_inc = $d_inc * 4;
196 my @groups;
197
198 my $group_tmp = new Panotools::Photos;
199 my $previous_time;
200 my $previous_inc = 0;
201 for my $image (@{$self})
202 {
203 my $datetime = $image->{exif}->{'DateTimeOriginal'} || $image->{exif}->{'FileModifyDate'};
204 my $time_unix = Image::ExifTool::GetUnixTime ($datetime);
205 $previous_time = $time_unix unless (defined $previous_time);
206 my $inc = $time_unix - $previous_time;
207
208 if (($inc - $previous_inc) > $d_inc)
209 {
210 push @groups, $group_tmp if ($group_tmp->AverageInterval < $max_inc);
211 $group_tmp = new Panotools::Photos;
212 }
213 push @{$group_tmp}, $image;
214
215 $previous_time = $time_unix;
216 $previous_inc = $inc;
217 }
218 push @groups, $group_tmp if ($group_tmp->AverageInterval < $max_inc);
219 return @groups;
220 }
221  
222 =pod
223  
224 Get the average time between shots:
225  
226 $average = $photos->AverageInterval;
227  
228 =cut
229  
230 sub AverageInterval
231 {
232 my $self = shift;
233 return 0 unless (scalar @{$self} > 1);
234  
235 my $start = $self->[0]->{exif}->{'DateTimeOriginal'} || $self->[0]->{exif}->{'FileModifyDate'};
236 my $end = $self->[-1]->{exif}->{'DateTimeOriginal'} || $self->[-1]->{exif}->{'FileModifyDate'};
237  
238 my $totaltime = Image::ExifTool::GetUnixTime ($end) - Image::ExifTool::GetUnixTime ($start);
239 return $totaltime / (scalar @{$self} -1);
240 }
241  
242 =item FOV FocalLength Rotation
243  
244 Get the Angle of View in degrees of the first photo:
245  
246 $photos->FOV;
247  
248 ..or any other photo (-1 is last):
249  
250 $photos->FOV (123);
251  
252 Returns undef if the FOV can't be calculated.
253  
254 =cut
255  
256 sub FOV
257 {
258 my $self = shift;
259 my $index = 0;
260 $index = shift if @_;
261 my $fov = $self->[$index]->{exif}->{'FOV'};
262 $fov =~ s/ .*$// if defined $fov;
263 return $fov;
264 }
265  
266 sub FocalLength
267 {
268 my $self = shift;
269 my $index = 0;
270 $index = shift if @_;
271 my $fl = $self->[$index]->{exif}->{'FocalLengthIn35mmFormat'};
272 $fl =~ s/ .*$// if defined $fl;
273 return $fl;
274 }
275  
276 sub Rotation
277 {
278 my $self = shift;
279 my $index = shift || 0;
280 my $rotation = $self->[$index]->{exif}->{'Rotation'} || undef;
281 return 0 unless $rotation;
282 return 0 if $rotation =~ /Mirror/;
283 return 0 if ($self->[$index]->{exif}->{'ImageWidth'}
284 < $self->[$index]->{exif}->{'ImageHeight'});
285 return 90 if $rotation =~ /Rotate 90 CW/;
286 return 180 if $rotation =~ /Rotate 180/;
287 return -90 if $rotation =~ /Rotate 270 CW/;
288 return 0;
289  
290 # 1 => 'Horizontal (normal)',
291 # 3 => 'Rotate 180',
292 # 6 => 'Rotate 90 CW',
293 # 8 => 'Rotate 270 CW',
294  
295 # 2 => 'Mirror horizontal',
296 # 4 => 'Mirror vertical',
297 # 5 => 'Mirror horizontal and rotate 270 CW',
298 # 7 => 'Mirror horizontal and rotate 90 CW',
299  
300 }
301  
302 =pod
303  
304 Get an EV value for a photo, this will be guessed from partial EXIF data:
305  
306 $photos->Eev ($index);
307  
308 =cut
309  
310 sub Eev
311 {
312 my $self = shift;
313 my $index = shift || 0;
314 my $exif = $self->[$index]->{exif};
315 my $aperture = $exif->{Aperture} || 1.0;
316 my $et = $exif->{ExposureTime} || $exif->{ShutterSpeed} || 1.0;
317 if ($et =~ /^1\/([0-9]+)$/) {$et = 1 / $1};
318 my $iso = $exif->{ISO} || 100;
319 # (A light value of 0 is defined as f/1.0 at 1 second with ISO 100)
320 return sprintf ('%.3f', (2*log ($aperture) - log($et) - log($iso/100)) / log(2));
321 }
322  
323 sub AverageRGB
324 {
325 my $self = shift;
326 my $RedBalance = 0;
327 my $GreenBalance = 0;
328 my $BlueBalance = 0;
329 my $count = 0;
330 for my $image (@{$self})
331 {
332 next unless ($image->{exif}->{'RedBalance'} and $image->{exif}->{'BlueBalance'});
333 $RedBalance += $image->{exif}->{'RedBalance'};
334 $GreenBalance += $image->{exif}->{'GreenBalance'} if defined $image->{exif}->{'GreenBalance'};
335 $BlueBalance += $image->{exif}->{'BlueBalance'};
336 $count++;
337 }
338 return (1,1,1) unless $count;
339 return ($RedBalance / $count, $GreenBalance / $count, $BlueBalance / $count);
340 }
341  
342 1;