-
Notifications
You must be signed in to change notification settings - Fork 33
/
gd_similar_images.pl
executable file
·205 lines (165 loc) · 4.82 KB
/
gd_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
#!/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 'bitwise';
use GD qw();
use List::Util qw(sum);
use File::Find qw(find);
use Getopt::Long qw(GetOptions);
GD::Image->trueColor(1);
my $width = 32;
my $height = 32;
my $percentage = 90;
my $keep_only = undef;
my $img_formats = '';
my $resize_to = $width . 'x' . $height;
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)
-r --resize-to=s : resize images to this resolution (default: $resize_to)
-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,
'r|resize-to=s' => \$resize_to,
'f|formats=s' => \$img_formats,
'k|keep=s' => \$keep_only,
'h|help' => sub { help(0) },
)
or die("Error in command line arguments");
($width, $height) = split(/\h*x\h*/i, $resize_to);
my $size = $width * $height;
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//) / $size)**2 * 100;
}
#>>>
sub fingerprint {
my ($image) = @_;
my $img = GD::Image->new($image) // return;
{
my $resized = GD::Image->new($width, $height);
$resized->copyResampled($img, 0, 0, 0, 0, $width, $height, $img->getBounds());
$img = $resized;
}
my @averages;
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
push @averages, sum($img->rgb($img->getPixel($x, $y)))/3;
}
}
my $avg = sum(@averages) / @averages;
join('', map { ($_ < $avg) ? 1 : 0 } @averages);
}
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}, $files[$j]{fingerprint});
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;