Skip to content

Commit 0a4aba2

Browse files
committed
new file: File Workers/collect_gifs.pl
new file: Image/gif2webp.pl
1 parent 22a179e commit 0a4aba2

File tree

9 files changed

+241
-48
lines changed

9 files changed

+241
-48
lines changed

File Workers/collect_gifs.pl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/perl
2+
3+
# Collect and move GIF images into a specific directory, by scanning a given a directory (and its subdirectories) for GIF images.
4+
5+
use 5.036;
6+
use File::Find qw(find);
7+
use File::Copy qw(move);
8+
use File::Path qw(make_path);
9+
use File::Basename qw(basename);
10+
use File::Spec::Functions qw(catfile curdir rel2abs);
11+
use Getopt::Long qw(GetOptions);
12+
13+
my $use_exiftool = 0; # true to use `exiftool` instead of `File::MimeInfo::Magic`
14+
15+
sub is_gif ($file) {
16+
17+
if ($use_exiftool) {
18+
my $res = `exiftool \Q$file\E`;
19+
20+
$? == 0 or return;
21+
defined($res) or return;
22+
23+
return ($res =~ m{^MIME\s+Type\s*:\s*image/gif}mi);
24+
}
25+
26+
require File::MimeInfo::Magic;
27+
(File::MimeInfo::Magic::magic($file) // '') eq 'image/gif';
28+
}
29+
30+
sub collect_gif ($file, $directory) {
31+
32+
my $dest = catfile($directory, basename($file));
33+
34+
if (-e $dest) {
35+
warn "File <<$dest>> already exists...\n";
36+
return;
37+
}
38+
39+
move($file, $dest);
40+
}
41+
42+
GetOptions('exiftool!' => \$use_exiftool,)
43+
or die "Error in command-line arguments!";
44+
45+
my @dirs = @ARGV;
46+
47+
@dirs || die "usage: perl $0 [directory | files]\n";
48+
49+
my $directory = rel2abs("GIF images"); # directory where to move the videos
50+
51+
if (not -d $directory) {
52+
make_path($directory)
53+
or die "Can't create directory <<$directory>>: $!";
54+
}
55+
56+
if (not -d $directory) {
57+
die "<<$directory>> is not a directory!";
58+
}
59+
60+
find(
61+
{
62+
wanted => sub {
63+
if (-f $_ and is_gif($_)) {
64+
say ":: Moving file: $_";
65+
collect_gif($_, $directory);
66+
}
67+
},
68+
},
69+
@dirs
70+
);

File Workers/collect_videos.pl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ ($file, $directory)
3232
move($file, $dest);
3333
}
3434

35+
my @dirs = @ARGV;
36+
37+
@dirs || die "usage: perl $0 [directory | files]\n";
38+
3539
my $directory = rel2abs("Videos"); # directory where to move the videos
3640

3741
if (not -d $directory) {
@@ -43,9 +47,6 @@ ($file, $directory)
4347
die "<<$directory>> is not a directory!";
4448
}
4549

46-
my @dirs = @ARGV;
47-
@dirs = curdir() if not @ARGV;
48-
4950
find(
5051
{
5152
wanted => sub {

Image/gif2webp.pl

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/perl
2+
3+
# Author: Trizen
4+
# Date: 14 October 2023
5+
# https://github.com/trizen
6+
7+
# Convert GIF animations to WEBP animations, using the `gif2webp` tool from "libwebp".
8+
9+
# The original GIF files are deleted.
10+
11+
use 5.036;
12+
use File::Find qw(find);
13+
use Getopt::Long qw(GetOptions);
14+
15+
my $gif2webp_cmd = "gif2webp"; # `gif2webp` command
16+
my $use_exiftool = 0; # true to use `exiftool` instead of `File::MimeInfo::Magic`
17+
18+
`$gif2webp_cmd -h`
19+
or die "Error: `$gif2webp_cmd` tool from 'libwebp' is not installed!\n";
20+
21+
sub gif2webp ($file) {
22+
23+
my $orig_file = $file;
24+
my $webp_file = $file;
25+
26+
if ($webp_file =~ s/\.gif\z/.webp/i) {
27+
## ok
28+
}
29+
else {
30+
$webp_file .= '.webp';
31+
}
32+
33+
if (-e $webp_file) {
34+
warn "[!] File <<$webp_file>> already exists...\n";
35+
next;
36+
}
37+
38+
system($gif2webp_cmd, '-lossy', $orig_file, '-o', $webp_file);
39+
40+
if ($? == 0 and (-e $webp_file) and ($webp_file ne $orig_file)) {
41+
unlink($orig_file);
42+
}
43+
else {
44+
return;
45+
}
46+
47+
return 1;
48+
}
49+
50+
sub determine_mime_type ($file) {
51+
52+
if ($file =~ /\.gif\z/i) {
53+
return "image/gif";
54+
}
55+
56+
if ($use_exiftool) {
57+
my $res = `exiftool \Q$file\E`;
58+
$? == 0 or return;
59+
defined($res) or return;
60+
if ($res =~ m{^MIME\s+Type\s*:\s*(\S+)}mi) {
61+
return $1;
62+
}
63+
return;
64+
}
65+
66+
require File::MimeInfo::Magic;
67+
File::MimeInfo::Magic::magic($file);
68+
}
69+
70+
my %types = (
71+
'image/gif' => {
72+
call => \&gif2webp,
73+
},
74+
);
75+
76+
GetOptions('exiftool!' => \$use_exiftool,)
77+
or die "Error in command-line arguments!";
78+
79+
@ARGV or die <<"USAGE";
80+
usage: perl $0 [options] [dirs | files]
81+
82+
options:
83+
84+
--exiftool : use `exiftool` to determine the MIME type (default: $use_exiftool)
85+
86+
USAGE
87+
88+
find(
89+
{
90+
no_chdir => 1,
91+
wanted => sub {
92+
93+
(-f $_) || return;
94+
my $type = determine_mime_type($_) // return;
95+
96+
if (exists $types{$type}) {
97+
$types{$type}{call}->($_);
98+
}
99+
}
100+
} => @ARGV
101+
);
102+
103+
say ":: Done!";

Image/magick_png2jpg.pl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ (@files)
6363

6464
sub determine_mime_type ($file) {
6565

66+
if ($file =~ /\.jpe?g\z/i) {
67+
return "image/jpeg";
68+
}
69+
70+
if ($file =~ /\.png\z/i) {
71+
return "image/png";
72+
}
73+
6674
if ($use_exiftool) {
6775
my $res = `exiftool \Q$file\E`;
6876
$? == 0 or return;

Image/optimize_images.pl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ (@files)
4141

4242
sub determine_mime_type ($file) {
4343

44+
if ($file =~ /\.jpe?g\z/i) {
45+
return "image/jpeg";
46+
}
47+
48+
if ($file =~ /\.png\z/i) {
49+
return "image/png";
50+
}
51+
4452
if ($use_exiftool) {
4553
my $res = `exiftool \Q$file\E`;
4654
$? == 0 or return;

Image/optimize_images_littleutils.pl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ (@files)
5252

5353
sub determine_mime_type ($file) {
5454

55+
if ($file =~ /\.jpe?g\z/i) {
56+
return "image/jpeg";
57+
}
58+
59+
if ($file =~ /\.png\z/i) {
60+
return "image/png";
61+
}
62+
5563
if ($use_exiftool) {
5664
my $res = `exiftool \Q$file\E`;
5765
$? == 0 or return;

Image/recompress_images.pl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ (%args)
8989

9090
sub determine_mime_type ($file) {
9191

92+
if ($file =~ /\.jpe?g\z/i) {
93+
return "image/jpeg";
94+
}
95+
96+
if ($file =~ /\.png\z/i) {
97+
return "image/png";
98+
}
99+
92100
if ($use_exiftool) {
93101
my $res = `exiftool \Q$file\E`;
94102
$? == 0 or return;

Image/webp2png.pl

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,47 @@
1212
use File::Find qw(find);
1313
use Getopt::Long qw(GetOptions);
1414

15-
my $batch_size = 100; # how many files to process at once
1615
my $dwebp_cmd = "dwebp"; # `dwebp` command
1716
my $use_exiftool = 0; # true to use `exiftool` instead of `File::MimeInfo::Magic`
1817

1918
`$dwebp_cmd -h`
20-
or die "Error: `dwebp` tool from 'libwebp' is not installed!\n";
19+
or die "Error: `$dwebp_cmd` tool from 'libwebp' is not installed!\n";
2120

22-
sub convert_WEBPs (@files) {
21+
sub webp2png ($file) {
2322

24-
say ":: Converting a batch of ", scalar(@files), " WEBP images...";
23+
my $orig_file = $file;
24+
my $png_file = $file;
2525

26-
foreach my $file (@files) {
27-
28-
my $orig_file = $file;
29-
my $png_file = $file;
30-
31-
if ($png_file =~ s/\.webp\z/.png/i) {
32-
## ok
33-
}
34-
else {
35-
$png_file .= '.png';
36-
}
26+
if ($png_file =~ s/\.webp\z/.png/i) {
27+
## ok
28+
}
29+
else {
30+
$png_file .= '.png';
31+
}
3732

38-
if (-e $png_file) {
39-
warn "[!] File <<$png_file>> already exists...\n";
40-
next;
41-
}
33+
if (-e $png_file) {
34+
warn "[!] File <<$png_file>> already exists...\n";
35+
next;
36+
}
4237

43-
system($dwebp_cmd, $orig_file, '-o', $png_file);
38+
system($dwebp_cmd, $orig_file, '-o', $png_file);
4439

45-
if ($? == 0 and (-e $png_file) and ($png_file ne $orig_file)) {
46-
unlink($orig_file);
47-
}
40+
if ($? == 0 and (-e $png_file) and ($png_file ne $orig_file)) {
41+
unlink($orig_file);
42+
}
43+
else {
44+
return;
4845
}
46+
47+
return 1;
4948
}
5049

5150
sub determine_mime_type ($file) {
5251

52+
if ($file =~ /\.webp\z/i) {
53+
return "image/webp";
54+
}
55+
5356
if ($use_exiftool) {
5457
my $res = `exiftool \Q$file\E`;
5558
$? == 0 or return;
@@ -66,21 +69,18 @@ ($file)
6669

6770
my %types = (
6871
'image/webp' => {
69-
files => [],
70-
call => \&convert_WEBPs,
71-
},
72+
call => \&webp2png,
73+
}
7274
);
7375

74-
GetOptions('exiftool!' => \$use_exiftool,
75-
'batch-size=i' => \$batch_size,)
76+
GetOptions('exiftool!' => \$use_exiftool,)
7677
or die "Error in command-line arguments!";
7778

7879
@ARGV or die <<"USAGE";
7980
usage: perl $0 [options] [dirs | files]
8081
8182
options:
8283
83-
--batch=i : how many files to process at once (default: $batch_size)
8484
--exiftool : use `exiftool` to determine the MIME type (default: $use_exiftool)
8585
8686
USAGE
@@ -94,25 +94,10 @@ ($file)
9494
my $type = determine_mime_type($_) // return;
9595

9696
if (exists $types{$type}) {
97-
98-
my $ref = $types{$type};
99-
push @{$ref->{files}}, $_;
100-
101-
if (scalar(@{$ref->{files}}) >= $batch_size) {
102-
$ref->{call}->(splice(@{$ref->{files}}));
103-
}
97+
$types{$type}{call}->($_);
10498
}
10599
}
106100
} => @ARGV
107101
);
108102

109-
foreach my $type (keys %types) {
110-
111-
my $ref = $types{$type};
112-
113-
if (@{$ref->{files}}) {
114-
$ref->{call}->(splice(@{$ref->{files}}));
115-
}
116-
}
117-
118103
say ":: Done!";

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ A nice collection of day-to-day Perl scripts.
152152
* [Tailz](./File%20Readers/tailz)
153153
* File Workers
154154
* [Auto extensions](./File%20Workers/auto_extensions.pl)
155+
* [Collect gifs](./File%20Workers/collect_gifs.pl)
155156
* [Collect videos](./File%20Workers/collect_videos.pl)
156157
* [Delete if exists](./File%20Workers/delete_if_exists.pl)
157158
* [Dir file updater](./File%20Workers/dir_file_updater.pl)
@@ -369,6 +370,7 @@ A nice collection of day-to-day Perl scripts.
369370
* [Gd png2jpg](./Image/gd_png2jpg.pl)
370371
* [Gd similar images](./Image/gd_similar_images.pl)
371372
* [Gd star trails](./Image/gd_star_trails.pl)
373+
* [Gif2webp](./Image/gif2webp.pl)
372374
* [Image2ascii](./Image/image2ascii.pl)
373375
* [Image2digits](./Image/image2digits.pl)
374376
* [Image2html](./Image/image2html.pl)

0 commit comments

Comments
 (0)