2023-11-09 20:26:26 -05:00
|
|
|
#!/usr/bin/env raku
|
|
|
|
|
|
|
|
#| resize images for uploading to gallery sites
|
|
|
|
unit sub MAIN(
|
|
|
|
#| number of concurrent jobs (default min{10, # cores})
|
|
|
|
Int :j(:$jobs) = min(10, $*KERNEL.cpu-cores),
|
2023-11-21 17:23:27 -05:00
|
|
|
#| formats to use for mastodon (png, webp, jpg, default, skip)
|
|
|
|
:m(:$mastodon) = 'default',
|
|
|
|
#| formats to use for itaku
|
|
|
|
:i(:$itaku) = 'default',
|
|
|
|
#| formats to use for furaffinity/weasyl
|
|
|
|
:f(:$furaffinity) = 'default',
|
|
|
|
#| formats to use for gallery.n.w
|
|
|
|
:g(:$gallery) = 'default',
|
2023-11-09 20:26:26 -05:00
|
|
|
#| webp/jpg quality setting (default 98)
|
|
|
|
Int :Q(:$quality) = 98,
|
|
|
|
#| only run conversions explicitly mentioned
|
|
|
|
Bool :O(:$only) = False,
|
|
|
|
Bool :n(:$dry-run) = False,
|
|
|
|
Bool :v(:$verbose) = False,
|
|
|
|
#| out dir, default ./out
|
2023-11-21 17:21:15 -05:00
|
|
|
IO(Str) :o(:$outdir) = 'out'.IO,
|
2024-01-11 14:33:35 -05:00
|
|
|
#| output as e.g. thing.mastodon.webp instead of mastodon/thing.webp
|
|
|
|
Bool :F(:$flat) = False,
|
|
|
|
Str :B(:$background) = 'white',
|
2023-11-09 20:26:26 -05:00
|
|
|
#| defaults to all *.png & *.jpg files in the current dir
|
|
|
|
*@files,
|
|
|
|
);
|
|
|
|
|
|
|
|
sub cmd(*@args) {
|
|
|
|
say "> @args[]" if $dry-run || $verbose;
|
|
|
|
run @args unless $dry-run;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub verbose(*@args) { say "> @args[]" if $verbose }
|
|
|
|
|
|
|
|
# fixup args
|
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
enum Format <png webp jpg>;
|
|
|
|
|
|
|
|
sub to-formats($_, *@default) {
|
|
|
|
when 'skip' { Empty }
|
|
|
|
when 'default' { $only ?? Empty !! @default }
|
|
|
|
when Format { $_ }
|
|
|
|
.lc.split(',').map: { Format::{$_} // die qq/unrecognised format "$_"/ }
|
|
|
|
}
|
|
|
|
|
2023-11-09 20:26:26 -05:00
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
my @mastodon = to-formats $mastodon, webp;
|
|
|
|
my @itaku = to-formats $itaku, webp;
|
|
|
|
my @furaffinity = to-formats $furaffinity, png, jpg;
|
|
|
|
my @gallery = to-formats $gallery, webp;
|
2023-11-09 20:26:26 -05:00
|
|
|
|
2024-01-11 14:33:35 -05:00
|
|
|
die "furaffinity/weasyl don't know what webp are" if @furaffinity.any == webp;
|
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
unless @files.=map(*.IO) {
|
|
|
|
@files = $*CWD.dir: test => /:i \.[jpe?g|png]$/ && *.IO.f;
|
|
|
|
}
|
2023-11-09 20:26:26 -05:00
|
|
|
for @files { die "$_ is not a regular file" unless .f; }
|
|
|
|
|
|
|
|
|
|
|
|
# running imagemagick (and oxipng sometimes) 🪄
|
|
|
|
|
2023-11-20 12:43:59 -05:00
|
|
|
sub convert($in, $name, $size, $format) {
|
2024-01-11 14:33:35 -05:00
|
|
|
my $out = $flat
|
|
|
|
?? $outdir.child($in.basename).extension("$name.$format")
|
|
|
|
!! $outdir.child($name).child($in.basename).extension("$format");
|
|
|
|
my @bg-flags = «-background $background -alpha remove» if $format == jpg;
|
2023-11-21 17:23:00 -05:00
|
|
|
cmd <mkdir -p>, $out.dirname;
|
2024-01-11 14:33:35 -05:00
|
|
|
cmd <convert>, $in, '-resize', $size, '-quality', $quality, @bg-flags, $out;
|
2023-11-09 20:26:26 -05:00
|
|
|
cmd <oxipng -q>, $out if $format == png;
|
2023-11-21 17:23:27 -05:00
|
|
|
$out
|
2023-11-09 20:26:26 -05:00
|
|
|
}
|
|
|
|
|
2024-01-11 14:33:35 -05:00
|
|
|
sub square($s) { "{$s}x{$s}>" }
|
2023-11-09 20:26:26 -05:00
|
|
|
|
|
|
|
my @sites = [
|
2024-01-11 14:33:35 -05:00
|
|
|
{:name<mastodon>, :formats(@mastodon), :size("{1280²}@")},
|
|
|
|
{:name<itaku>, :formats(@itaku), :size(square 2800)},
|
|
|
|
{:name<furaffinity>, :formats(@furaffinity), :size(square 1500)},
|
|
|
|
{:name<gallery>, :formats(@gallery), :size(square 3000)},
|
2023-11-21 17:23:27 -05:00
|
|
|
];
|
2023-11-09 20:26:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
# go go go
|
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
my @tasks;
|
|
|
|
|
|
|
|
sub task-serial(&body, :$if = True) { body if $if; }
|
|
|
|
|
|
|
|
sub task-parallel(&body, :$if = True) {
|
|
|
|
state $sem = Semaphore.new: $jobs;
|
|
|
|
return unless $if;
|
|
|
|
my $task = start { $sem.acquire; my $res = body; $sem.release; $res }
|
|
|
|
@tasks.push: $task;
|
|
|
|
$task
|
2023-11-09 20:26:26 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
constant &task = $dry-run ?? &task-serial !! &task-parallel;
|
|
|
|
|
|
|
|
my @todo = (@files X @sites).map: -> ($file, %rest) { hash :$file, %rest };
|
|
|
|
my $len = @todo.map(*.<formats>).sum;
|
|
|
|
my $index;
|
|
|
|
|
|
|
|
for @todo -> (:$file, :$name, :@formats, :$size) {
|
|
|
|
my @outputs = @formats.map: -> $format {
|
|
|
|
my $i = ++$index; my $base = $file.basename;
|
|
|
|
task {
|
|
|
|
say "[$i/$len] $name $base ($format)";
|
|
|
|
convert $file, $name, $size, $format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
task if => !$dry-run && @outputs > 1, {
|
|
|
|
my $keep = @outputs».=result.map({$_ => .s}).min(*.value).key;
|
|
|
|
verbose "# keeping $keep";
|
|
|
|
for @outputs { next when $keep; cmd "rm", $_ }
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 20:26:26 -05:00
|
|
|
|
2023-11-21 17:23:27 -05:00
|
|
|
@tasks».result;
|