#!/usr/bin/env perl use Shell::Tools::Extra Shell => [qw/ :FATAL convert gs /]; use autodie ':all'; use File::stat; =head1 SYNOPSIS shrink FILENAMEs... =head1 DESCRIPTION Shrinks images and PDFs using ImageMagick resp. Ghostscript. Output files will have the same name but with F<_sm> appended, for example F becomes F. This script will not overwrite existing files. Place this script in C<< ~/.local/share/nautilus/scripts/ >> for it to show up in the Nautilus right-click menu. =cut getopts('', \my %opts) or pod2usage; warn "no filenames specified\n" unless @ARGV; for my $f (@ARGV) { if (not -e -f $f) { warn "doesn't exist or isn't a file, skipping: $f\n"; next } my ($bn,undef,$ext) = fileparse $f, qr/\.[^\.]+$/; my ($mode,$of); if ( $ext=~/^\.(?:jpe?g|png|gif)$/i ) { $mode = 'img'; $of = file($f)->dir->file($bn."_sm.jpg"); } elsif ( $ext=~/^\.pdf$/ ) { $mode = 'pdf'; $of = file($f)->dir->file($bn."_sm".$ext); } else { warn "I don't handle the extension $ext, skipping: $f\n"; next } if (-e $of) { warn "output already exists, skipping: $of\n"; next } if ( $bn=~/_sm$/ ) { warn "appears to already be shrunk, skipping: $f\n"; next } if ($mode eq 'img') { convert "$f", qw{ -auto-orient -resize 1000x1000> -strip -quality 95 }, "$of" } elsif ($mode eq 'pdf') { gs qw{ -q -dBATCH -dNOPAUSE -dSAFER -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dAutoRotatePages=/None }, "-sOutputFile=$of", "$f" } else { die "internal error '$mode'" } chmod stat($f)->mode, $of; } =head1 AUTHOR, COPYRIGHT, AND LICENSE Copyright (c) 2017 Hauke Daempfling (haukex@zero-g.net) at the Leibniz Institute of Freshwater Ecology and Inland Fisheries (IGB), Berlin, Germany, L This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see L. =cut