-
Notifications
You must be signed in to change notification settings - Fork 33
/
img-autocrop.pl
executable file
·277 lines (219 loc) · 7.32 KB
/
img-autocrop.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 14 June 2015
# https://github.com/trizen
# A generic image auto-cropper which adapt itself to any background color.
use 5.010;
use strict;
use warnings;
use GD qw();
use Getopt::Long qw(GetOptions);
use File::Basename qw(basename);
use File::Spec::Functions qw(catfile);
# Set true color
GD::Image->trueColor(1);
# Autoflush mode
local $| = 1;
my $tolerance = 5;
my $invisible = 0;
my $jpeg_quality = 95;
my $png_compression = 7;
my $directory = 'Cropped images';
sub help {
my ($code) = @_;
print <<"EOT";
usage: $0 [options] [images]
options:
-t --tolerance=i : tolerance value for the background color
default: $tolerance
-i --invisible! : make the background transparent after cropping
default: ${$invisible ? \'true' : \'false'}
-p --png-compress=i : the compression level for PNG images
default: $png_compression
-j --jpeg-quality=i : the quality value for JPEG images
default: $jpeg_quality
-d --directory=s : directory where to create the cropped images
default: "$directory"
example:
perl $0 -t 10 *.png
EOT
exit($code // 0);
}
GetOptions(
'd|directory=s' => \$directory,
'i|invisible!' => \$invisible,
't|tolerance=i' => \$tolerance,
'p|png-compression=i' => \$png_compression,
'j|jpeg-quality=i' => \$jpeg_quality,
'h|help' => sub { help(0) },
)
or die("$0: error in command line arguments!\n");
{
my %cache;
sub is_background {
my ($img, $index, $bg_rgb) = @_;
my $rgb = ($cache{$index} //= [$img->rgb($index)]);
abs($rgb->[0] - $bg_rgb->[0]) <= $tolerance
and abs($rgb->[1] - $bg_rgb->[1]) <= $tolerance
and abs($rgb->[2] - $bg_rgb->[2]) <= $tolerance;
}
}
sub check {
my ($img, $bg_rgb, $width, $height) = @_;
my $check = sub {
foreach my $sub (@_) {
is_background($img, $sub->(), $bg_rgb) || return;
}
1;
};
my $w_lt_h = $width < $height;
my $min = $w_lt_h ? $width : $height;
my %seen;
# Spiral-in to smaller gaps
# -- this algorithm needs to be improved --
for (my $i = int(sqrt($min)) ; $i >= 1 ; $i--) {
foreach my $j (1 .. $min) {
next if $j % $i;
next if $seen{$j}++;
if (
not $check->(
sub { $img->getPixel($j, 0) },
sub { $img->getPixel(0, $j) },
sub { $img->getPixel($j, $height) },
sub { $img->getPixel($width, $j) },
)
) {
return;
}
}
}
if ($w_lt_h) {
foreach my $y ($width + 1 .. $height) {
if (not $check->(sub { $img->getPixel(0, $y) }, sub { $img->getPixel($width, $y) })) {
return;
}
}
}
else {
foreach my $x ($height + 1 .. $width) {
if (not $check->(sub { $img->getPixel($x, 0) }, sub { $img->getPixel($x, $height) })) {
return;
}
}
}
return 1;
}
sub make_invisible_bg {
my ($img, $transparent, $bg_rgb, $width, $height) = @_;
foreach my $x (0 .. $width) {
foreach my $y (0 .. $height) {
if (is_background($img, $img->getPixel($x, $y), $bg_rgb)) {
$img->setPixel($x, $y, $transparent);
}
}
}
}
sub autocrop {
my @images = @_;
foreach my $file (@images) {
my $img = GD::Image->new($file);
if (not defined $img) {
warn "[!] Can't process image `$file': $!\n";
next;
}
my ($width, $height) = $img->getBounds();
$width -= 1;
$height -= 1;
my $bg_rgb = [$img->rgb($img->getPixel(0, 0))];
print "Checking: $file";
check($img, $bg_rgb, $width, $height) || do {
say " - fail!";
next;
};
say " - ok!";
print "Cropping: $file";
my $top;
my $bottom;
TB: foreach my $y (1 .. $height) {
foreach my $x (1 .. $width) {
if (not defined $top) {
if (not is_background($img, $img->getPixel($x, $y), $bg_rgb)) {
$top = $y - 1;
}
}
if (not defined $bottom) {
if (not is_background($img, $img->getPixel($x, $height - $y), $bg_rgb)) {
$bottom = $height - $y + 1;
}
}
if (defined $top and defined $bottom) {
last TB;
}
}
}
if (not defined $top or not defined $bottom) {
say " - fail!";
next;
}
my $left;
my $right;
LR: foreach my $x (1 .. $width) {
foreach my $y (1 .. $height) {
if (not defined $left) {
if (not is_background($img, $img->getPixel($x, $y), $bg_rgb)) {
$left = $x - 1;
}
}
if (not defined $right) {
if (not is_background($img, $img->getPixel($width - $x, $y), $bg_rgb)) {
$right = $width - $x + 1;
}
}
if (defined $left and defined $right) {
last LR;
}
}
}
if (not defined $left or not defined $right) {
say " - fail!";
next;
}
my $cropped = GD::Image->new($right - $left + 1, $bottom - $top + 1);
my $index;
if ($invisible) {
$index = $cropped->colorAllocateAlpha(int(rand(256)), int(rand(256)), int(rand(256)), 0);
$cropped->filledRectangle(0, 0, $cropped->width, $cropped->height, $index);
$cropped->transparent($index);
}
$cropped->copyResized(
$img,
0, # destX
0, # destY
$left, # srcX
$top, # srcY
$right, # destW
$bottom, # destH
$right, # srcW
$bottom, # srcH
);
my $name = catfile($directory, basename($file));
if ($invisible) {
make_invisible_bg($cropped, $index, $bg_rgb, $cropped->width - 1, $cropped->height - 1);
$name =~ s/\.\w+\z/.png/;
}
open my $fh, '>:raw', $name or die "Can't create file `$name': $!";
print $fh (
$name =~ /\.png\z/i ? $cropped->png($png_compression)
: $name =~ /\.gif\z/i ? $cropped->gif
: $cropped->jpeg($jpeg_quality)
);
close $fh;
say " - ok!";
}
}
@ARGV || help(1);
if (not -d $directory) {
mkdir($directory) || die "Can't mkdir `$directory': $!";
}
autocrop(@ARGV);