]> mj.ucw.cz Git - gallery.git/blob - gal/bin/gal-scan
gal-mj-digikam imports GPS coordinates
[gallery.git] / gal / bin / gal-scan
1 #!/usr/bin/perl
2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 use UCW::Gallery;
9 use UCW::Gallery::Hashes;
10 use File::Spec;
11 use Image::EXIF;
12 use Getopt::Long;
13
14 if (@ARGV && $ARGV[0] eq '--help') {
15         die <<AMEN ;
16 Usage:  cat list | gal scan <options>
17    or:  gal scan <options> <files and directories>
18    or:  gal scan <options> --update
19
20 Options:
21 --add           Keep existing images and add new ones
22 AMEN
23 }
24
25 my $add;
26 my $update;
27 GetOptions(
28         'add!' => \$add,
29         'update!' => \$update,
30 ) or die "Try gal scan --help\n";
31
32 STDOUT->autoflush(1);
33 my $gal = UCW::Gallery->load_config;
34 my $orig_prefix = $gal->get('OrigDir');
35 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
36
37 my @source = ();
38 if ($update) {
39         print "Loading previous gallery.list\n";
40         my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
41         @source = @{$pg};
42 } elsif (@ARGV) {
43         for my $in (@ARGV) {
44                 if (-f $in) {
45                         push @source, { file => $in };
46                 } elsif (-d $in) {
47                         opendir D, $in or die "Cannot scan directory $in: $!\n";
48                         my @p = ();
49                         while (my $e = readdir D) {
50                                 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
51                                 if ($f =~ m{\.(jpe?g|png)$}i) {
52                                         push @p, $f;
53                                 }
54                         }
55                         closedir D;
56                         push @source, map { { file => $_ } } sort @p;
57                 } else {
58                         die "$in is neither file nor directory\n";
59                 }
60         }
61 } else {
62         binmode STDIN, ':utf8';
63         @source = @{$gal->read_list_fh(\*STDIN)};
64 }
65
66 my $hashes = UCW::Gallery::Hashes->new($gal);
67
68 print "Scanning photos\n";
69 my @images = ();
70 foreach my $src (@source) {
71         my $name = $src->{file};
72         if ($name =~ m{^/}) {
73                 # Try to relativize to OrigDir
74                 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
75                         $src->{file} = $name = substr($name, length $orig_prefix);
76                 }
77         }
78         print "\t$name:";
79
80         my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
81         -f $path or die "Cannot find $path\n";
82
83         $src->{id} = $hashes->hash_image($path);
84         print " id=", $src->{id};
85
86         if (!defined $src->{orientation} || $src->{orientation} eq '-') {
87                 my $e = new Image::EXIF($path);
88                 my $i = $e->get_all_info();
89                 if ($e->error) {
90                         print "EXIF error: ", $e->error, "\n";
91                         $src->{orientation} = '.';
92                 } else {
93                         # print STDERR Dumper($i), "\n";
94                         my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
95                         if ($o eq "Top, Left-Hand") { $o = "."; }
96                         elsif ($o eq "Right-Hand, Top") { $o = "r"; }
97                         elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
98                         elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
99                         else {
100                                 print "Unrecognized orientation: $o\n";
101                                 $o = ".";
102                         }
103                         $src->{orientation} = $o;
104                 }
105         }
106         print " ori=", $src->{orientation};
107
108         defined $src->{xf} or $src->{xf} = $gal->get('ScanDefaultTransform');
109         print " xfrm=", $src->{xf};
110
111         $src->{title} //= '';
112         push @images, $src;
113         print "\n";
114 }
115
116 if (!$update) {
117         my $old = $gal->read_list('gallery.list');
118         if ($old) {
119                 print "Updating gallery.list\n";
120                 my %new_by_id = map { $_->{id} => $_ } @images;
121                 my @result = ();
122                 for my $o (@$old) {
123                         my $id = $o->{id};
124                         my $i = $new_by_id{$id};
125                         if (!$i) {
126                                 if ($add) {
127                                         print "\t$id: kept\n";
128                                         push @result, $o;
129                                 } else {
130                                         print "\t$id: removed\n";
131                                 }
132                         } else {
133                                 print "\t$id: kept\n";
134                                 push @result, $o;
135                                 delete $new_by_id{$id};
136                         }
137                 }
138                 for my $i (@images) {
139                         my $id = $i->{id};
140                         $new_by_id{$id} or next;
141                         print "\t$id: new\n";
142                         push @result, $i;
143                 }
144                 @images = @result;
145         }
146 }
147
148 $gal->write_list('gallery.list', \@images);
149 print "Written gallery.list (", (scalar @images), " items)\n";
150 $hashes->write;