I'm told that JPEG is at its most efficient if the image dimensions are exact multiple of 8 pixels. This program crops a PBM/PGM/PPM image to the nearest 8x8 tile multiple, or to any tile size you specify on the command line.

Includes a small hack to identify a PNM file without using pnmfile(1). Needs the pnmcut(1) tool.

Update 27/12/02: Removed use diagnostics; on the advice of tachyon.

#!/usr/bin/env perl # pnmjpegcrop - trim a pnm to 8 by 8, or whatever specified on cmd lin +e # created by stewart on 02002/10/25 # reads a whole PNM file from stdin into core, so a bit more of a # hog than it should be. Writes suitably trimmed PNM stream to stdout. use strict; use warnings; use integer; # hey, I actually use this here! my $modulo=8; # real default # scan arguments for something like -NN for default trim tile size if ($#ARGV > -1) { if ($ARGV[0] =~ /^\-+[0-9]+/) { $modulo=shift(@ARGV); $modulo =~ s/\D+//g; } } $modulo=8 unless (defined($modulo)); # sanity checks $modulo=1 if ($modulo < 1); my @file=<>; # slurp all input my @pnminfo=(); # read pnm header; could use pnmfile(1) or identify(1) for this # but since pnm is such a simple format ... for (my $i=0; $i <=$#file; $i++) { $_=$file[$i]; # don't use foreach() as it modifies @ +file s/^\s+//; # clean leading w/s next if (m/^\#/); # skip comments last if ($#pnminfo == 2); # exit if we've read three things s/\s+$//; # normalize rest of w/s s/\s+/ /g; my @a=split(' ', $_); push @pnminfo, @a; } my ($type, $width, $height) = @pnminfo; die "Input is not PNM\n" unless ($type =~ m/^P\d$/); # die on wrong in +put # tile mustn't be > width or height ($modulo)=sort {$a <=> $b} ($modulo, $width, $height); my $iwidth=($width/$modulo)*$modulo; # these are integer divides, reme +mber my $iheight=($height/$modulo)*$modulo; my $xoff=($width-$iwidth)/2; # choose offset for as near centred as + we can my $yoff=($height-$iheight)/2; if (($width == $iwidth) and ($height == $iheight)) { # file already a multiple of $modulo x $modulo pixels, so leave it + alone print @file; # splat whole file out to stdout } else { # use old-style pnmcut arguments so we're compatible # should probably be more careful with pnmcut path for non-trusted + use. open(PNMCUT, "| pnmcut $xoff $yoff $iwidth $iheight") or die "pnmcut failed: $!\n"; print PNMCUT @file; # splat whole file out through pnmcut close(PNMCUT); } exit;

In reply to Crop PNM files for JPEG conversion by Willard B. Trophy

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.