Here's a script I use all the time via the right-click menu in my file manager (Nautilus), it will shrink PDFs and images so they work better as email attachments (Update: to be a little more specific: files that are several MB or more are usually due to high-resolution images, so this script runs commands to reduce their resolution). It does require Path::Class, ImageMagick's convert, GhostScript's gs, and my modules IPC::Run3::Shell and Shell::Tools.
#!/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<foo.jpg> becomes F<foo_sm.jpg>. 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 -qu +ality 95 }, "$of" } elsif ($mode eq 'pdf') { gs qw{ -q -dBATCH -dNOPAUSE -dSAFER -sDEVICE=pdfwrite -dPDFS +ETTINGS=/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 (I +GB), Berlin, Germany, L<http://www.igb-berlin.de/> 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<http://www.gnu.org/licenses/>. =cut
(Also, not Perl related, but I use this pretty much every day: setting up a keyboard combination such as Ctrl+Shift+F for the shell command xsel -b | xsel -ib, which causes the current clipboard buffer to be converted to text-only, which has been incredibly useful when copying formatted text that I don't want to keep the formatting of. The shell command may need to be placed into a simple script file depending on whether you can configure keyboard shortcuts to run a shell command, or if they can only run a single executable.)
My 3000th node! :-O
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Shrink Images and PDFs
by parv (Parson) on May 02, 2020 at 11:08 UTC | |
by haukex (Archbishop) on May 02, 2020 at 11:52 UTC | |
by parv (Parson) on May 02, 2020 at 13:02 UTC | |
by haukex (Archbishop) on May 02, 2020 at 14:47 UTC | |
Re: Shrink Images and PDFs
by anita2R (Scribe) on May 12, 2020 at 11:43 UTC | |
by haukex (Archbishop) on May 12, 2020 at 16:47 UTC | |
Re: Shrink Images and PDFs
by perlfan (Vicar) on Jun 13, 2020 at 04:25 UTC | |
by haukex (Archbishop) on Jun 13, 2020 at 14:24 UTC | |
by perlfan (Vicar) on Jun 13, 2020 at 17:28 UTC | |
by marto (Cardinal) on Jun 13, 2020 at 18:38 UTC | |
by haukex (Archbishop) on Jun 13, 2020 at 19:35 UTC | |
by perlfan (Vicar) on Jun 13, 2020 at 19:31 UTC | |
|