-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathimg-autocrop-whitebg.pl
executable file
·180 lines (142 loc) · 4.37 KB
/
img-autocrop-whitebg.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 14 June 2015
# https://github.com/trizen
# Auto-crop a list of images that have a white background.
use 5.010;
use strict;
use warnings;
use GD qw();
use File::Basename qw(basename);
use File::Spec::Functions qw(catfile);
# Set true color
GD::Image->trueColor(1);
# Autoflush mode
local $| = 1;
my $dir = 'Cropped images';
sub check {
my ($img, $width, $height) = @_;
my $check = sub {
foreach my $sub (@_) {
$sub->() == 0 or 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 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;
print "Checking: $file";
check($img, $width, $height) || do {
print " - fail!\n";
next;
};
print " - ok!\n";
print "Cropping: $file";
my $top;
my $bottom;
TB: foreach my $y (1 .. $height) {
foreach my $x (1 .. $width) {
if (not defined $top) {
if ($img->getPixel($x, $y)) {
$top = $y - 1;
}
}
if (not defined $bottom) {
if ($img->getPixel($x, $height - $y)) {
$bottom = $height - $y + 1;
}
}
if (defined $top and defined $bottom) {
last TB;
}
}
}
my $left;
my $right;
LR: foreach my $x (1 .. $width) {
foreach my $y (1 .. $height) {
if (not defined $left) {
if ($img->getPixel($x, $y)) {
$left = $x - 1;
}
}
if (not defined $right) {
if ($img->getPixel($width - $x, $y)) {
$right = $width - $x + 1;
}
}
if (defined $left and defined $right) {
last LR;
}
}
}
my $cropped = GD::Image->new($right - $left + 1, $bottom - $top + 1);
$cropped->copyResized(
$img,
0, # destX
0, # destY
$left, # srcX
$top, # srcY
$right, # destW
$bottom, # destH
$right, # srcW
$bottom, # srcH
);
my $name = catfile($dir, basename($file));
open my $fh, '>:raw', $name or die "Can't create file `$name': $!";
print $fh ($name =~ /\.png\z/i ? $cropped->png : $cropped->jpeg);
close $fh;
print " - ok!\n";
}
}
@ARGV || die "usage: $0 [images]\n";
if (not -d $dir) {
mkdir($dir) || die "Can't mkdir `$dir': $!";
}
autocrop(@ARGV);