Compare commits

...

4 Commits

Author SHA1 Message Date
rhiannon morris dc0081be69 can try multiple formats per site
with a lot of refactoring, because the 'pick best' task needs to wait
for the resizing tasks
2023-11-21 23:24:30 +01:00
rhiannon morris 36a3338ff2 add --subdirs 2023-11-21 23:23:00 +01:00
rhiannon morris 4242dfe096 MAIN can't actually take IO arguments 2023-11-21 23:21:15 +01:00
rhiannon morris 4093b1ceda add gallery site stuff 2023-11-20 18:43:59 +01:00
1 changed files with 77 additions and 35 deletions

View File

@ -1,17 +1,17 @@
#!/usr/bin/env raku
enum Format <DEFAULT skip png webp jpg>;
#| 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),
#| format to use for mastodon (png, webp, jpg, skip)
Format :m(:$mastodon) is copy = DEFAULT,
#| format to use for itaku
Format :i(:$itaku) is copy = DEFAULT,
#| format to use for furaffinity/weasyl
Format :f(:$furaffinity) is copy = DEFAULT,
#| 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',
#| webp/jpg quality setting (default 98)
Int :Q(:$quality) = 98,
#| only run conversions explicitly mentioned
@ -19,7 +19,9 @@ unit sub MAIN(
Bool :n(:$dry-run) = False,
Bool :v(:$verbose) = False,
#| out dir, default ./out
IO :o(:$outdir) = 'out'.IO,
IO(Str) :o(:$outdir) = 'out'.IO,
#| use a subdirectory for each site
Bool :d(:$subdirs) = False,
#| defaults to all *.png & *.jpg files in the current dir
*@files,
);
@ -33,48 +35,88 @@ sub verbose(*@args) { say "> @args[]" if $verbose }
# fixup args
die "furaffinity/weasyl don't know what webp are" if $furaffinity == webp;
enum Format <png webp jpg>;
$mastodon ||= $only ?? skip !! webp;
$itaku ||= $only ?? skip !! webp;
$furaffinity ||= $only ?? skip !! png;
sub to-formats($_, *@default) {
when 'skip' { Empty }
when 'default' { $only ?? Empty !! @default }
when Format { $_ }
.lc.split(',').map: { Format::{$_} // die qq/unrecognised format "$_"/ }
}
(@files .= map: *.IO) ||= $*CWD.dir: test => /:i \.[jpe?g|png]$/;
die "furaffinity/weasyl don't know what webp are" if $furaffinity ~~ /:i webp/;
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;
unless @files.=map(*.IO) {
@files = $*CWD.dir: test => /:i \.[jpe?g|png]$/ && *.IO.f;
}
for @files { die "$_ is not a regular file" unless .f; }
# running imagemagick (and oxipng sometimes) 🪄
sub convert($in, $site, $format, *@flags) {
my $out = $outdir.child($in.basename).extension("$site.$format");
cmd <convert>, $in, @flags, $out;
sub convert($in, $name, $size, $format) {
my $out = $subdirs
?? $outdir.child($name).child($in.basename).extension("$format")
!! $outdir.child($in.basename).extension("$name.$format");
cmd <mkdir -p>, $out.dirname;
cmd <convert>, $in, '-resize', $size, '-quality', $quality, $out;
cmd <oxipng -q>, $out if $format == png;
$out
}
constant $SIZE = 2800;
constant $SQUARE = "{$SIZE}x{$SIZE}";
constant $MASTO-SIZE = "{1280**2}@";
constant $SIZE = 2800;
constant $SQUARE = "{$SIZE}x{$SIZE}>";
constant $MASTO-SIZE = "{1280²}@";
constant $GALLERY-SIZE = 3000;
constant $GALLERY-SQUARE = "{$GALLERY-SIZE}x{$GALLERY-SIZE}>";
my @sites = [
{:name<mastodon>, :format($mastodon), :size($MASTO-SIZE)},
{:name<itaku>, :format($itaku), :size($SQUARE)},
{:name<furaffinity>, :format($furaffinity), :size($SQUARE)},
].grep: *<format> != skip;
{:name<mastodon>, :formats(@mastodon), :size($MASTO-SIZE)},
{:name<itaku>, :formats(@itaku), :size($SQUARE)},
{:name<furaffinity>, :formats(@furaffinity), :size($SQUARE)},
{:name<gallery>, :formats(@gallery), :size($GALLERY-SQUARE)},
];
# go go go
sub parallel(@vals, &f) {
my $sem = Semaphore.new: $jobs;
my @tasks = @vals.map: { start { $sem.acquire; f $_; $sem.release } };
@tasks».result;
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
}
cmd <mkdir -p>, $outdir;
constant &task = $dry-run ?? &task-serial !! &task-parallel;
my @todo = @files X @sites;
parallel @todo, -> ($file, % (:$name, :$format, :$size)) {
state $i++;
say "[$i/@todo.elems()] $name $file.basename()";
convert $file, $name, $format, '-resize', $size, '-quality', $quality;
};
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", $_ }
}
}
@tasks».result;