-
Notifications
You must be signed in to change notification settings - Fork 33
/
imager_similar_images.pl
executable file
·212 lines (173 loc) · 5.23 KB
/
imager_similar_images.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 26 August 2015
# Edit: 24 October 2023
# Website: https://github.com/trizen
# Find images that look similar.
# Blog post:
# https://trizenx.blogspot.com/2015/08/finding-similar-images.html
use 5.022;
use strict;
use warnings;
use experimental qw(bitwise);
use Imager qw();
use List::Util qw(sum);
use File::Find qw(find);
use Getopt::Long qw(GetOptions);
my $width = 32;
my $height = 'auto';
my $percentage = 90;
my $keep_only = undef;
my $img_formats = '';
my @img_formats = qw(
jpeg
jpg
png
);
sub help {
my ($code) = @_;
local $" = ",";
print <<"EOT";
usage: $0 [options] [dir]
options:
-p --percentage=i : minimum similarity percentage (default: $percentage)
-w --width=i : resize images to this width (default: $width)
-h --height=i : resize images to this height (default: $height)
-f --formats=s,s : specify more image formats (default: @img_formats)
-k --keep=s : keep only the 'smallest' or 'largest' image from each group
WARNING: option '-k' permanently removes your images!
example:
perl $0 -p 75 -r '8x8' ~/Pictures
EOT
exit($code);
}
GetOptions(
'p|percentage=i' => \$percentage,
'w|width=s' => \$width,
'h|height=s' => \$height,
'f|formats=s' => \$img_formats,
'k|keep=s' => \$keep_only,
)
or die("Error in command line arguments");
push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
my $img_formats_re = do {
local $" = '|';
qr/\.(@img_formats)\z/i;
};
#<<<
sub alike_percentage {
((($_[0] ^. $_[1]) =~ tr/\0//) / $_[2])**2 * 100;
}
#>>>
sub fingerprint {
my ($image) = @_;
my $img = Imager->new(file => $image) or do {
warn "Failed to load <<$image>>: ", Imager->errstr();
return;
};
if ($height ne 'auto') {
$img = $img->scale(ypixels => $height, qtype => 'preview');
}
else {
$img = $img->scale(xpixels => $width, qtype => 'preview');
}
my ($curr_width, $curr_height) = ($img->getwidth, $img->getheight);
my @averages;
foreach my $y (0 .. $curr_height - 1) {
my @line = $img->getscanline(y => $y);
foreach my $pixel (@line) {
my ($R, $G, $B) = $pixel->rgba;
push @averages, sum($R, $G, $B) / 3;
}
}
my $avg = sum(@averages) / @averages;
[join('', map { ($_ < $avg) ? 1 : 0 } @averages), $curr_width, $curr_height];
}
sub find_similar_images(&@) {
my $callback = shift;
my @files;
find {
no_chdir => 1,
wanted => sub {
(/$img_formats_re/o && -f) || return;
push @files,
{
fingerprint => fingerprint($_) // return,
filename => $_,
};
}
} => @_;
#
## Populate the %alike hash
#
my %alike;
foreach my $i (0 .. $#files - 1) {
for (my $j = $i + 1 ; $j <= $#files ; $j++) {
my $p = alike_percentage(
$files[$i]{fingerprint}->[0],
$files[$j]{fingerprint}->[0],
sqrt($files[$i]{fingerprint}->[1] * $files[$j]{fingerprint}->[1]) * sqrt($files[$i]{fingerprint}->[2] * $files[$j]{fingerprint}->[2])
);
if ($p >= $percentage) {
$alike{$files[$i]{filename}}{$files[$j]{filename}} = $p;
$alike{$files[$j]{filename}}{$files[$i]{filename}} = $p;
}
}
}
#
## Group the files
#
my @alike;
foreach my $root (
map { $_->[0] }
sort { ($a->[1] <=> $b->[1]) || ($b->[2] <=> $a->[2]) }
map {
my $keys = keys(%{$alike{$_}});
my $avg = sum(values(%{$alike{$_}})) / $keys;
[$_, $keys, $avg]
}
keys %alike
) {
my @group = keys(%{$alike{$root}});
if (@group) {
my $avg = 0;
$avg += delete($alike{$_}{$root}) for @group;
push @alike, {score => $avg / @group, files => [$root, @group]};
}
}
#
## Callback each group
#
my %seen;
foreach my $group (sort { $b->{score} <=> $a->{score} } @alike) {
(@{$group->{files}} == grep { $seen{$_}++ } @{$group->{files}}) and next;
$callback->($group->{score}, $group->{files});
}
return 1;
}
@ARGV || help(1);
find_similar_images {
my ($score, $files) = @_;
printf("=> Similarity: %.0f%%\n", $score);
say join("\n", sort @{$files});
say "-" x 80;
if (defined($keep_only)) {
my @existent_files = grep { -f $_ } @$files;
scalar(@existent_files) > 1 or return;
my @sorted_by_size = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -s $_] } @existent_files;
if ($keep_only =~ /large/i) {
pop(@sorted_by_size);
}
elsif ($keep_only =~ /small/i) {
shift(@sorted_by_size);
}
else {
die "error: unknown value <<$keep_only>> for option `-k`!\n";
}
foreach my $file (@sorted_by_size) {
say "Removing: $file";
unlink($file) or warn "Failed to remove: $!";
}
}
} @ARGV;