Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Shrink Images and PDFs by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found