Probably best way is one of the above (Image::Magick or gimp), but as mentioned by rduke15, you have to go through pixels, but not all of them. I actualy did this recently, but in a C# program. The code is below if it helps at all as pseudocode. It takes a couple shortcuts in the loop bounds to try to check the minimum number of pixels.
Bitmap canvas = new Bitmap("C:\\WINNT\\BgInfo.bmp"); Color bgcolor = canvas.GetPixel(5,5); int left = 0; for(int x=0; x< canvas.Width; x++){ for(int y=0; y< canvas.Height; y++){ if( canvas.GetPixel(x,y) != bgcolor ){ left = x; break; } } if( left > 0 ){ break; } } int top = 0; for(int y=0; y< canvas.Height; y++){ for(int x=left; x< canvas.Width; x++){ if( canvas.GetPixel(x,y) != bgcolor ){ top = y; break; } } if( top > 0 ){ break; } } int right = 0; for(int x = canvas.Width-1; x>=left; x--){ for(int y = canvas.Height-1; y>=top; y--){ if( canvas.GetPixel(x,y) != bgcolor ){ right = x; break; } } if( right > 0 ){ break; } } int bottom = 0; for(int y = canvas.Height-1; y>=top; y--){ for(int x = right; x>=left; x--){ if( canvas.GetPixel(x,y) != bgcolor ){ bottom = y; break; } } if( bottom > 0 ){ break; } } int pad = 10; left -= pad; top -= pad; right += pad; bottom += pad; if( top>0 && left>0 && right>0 && bottom>0 && right > left + && bottom > top ){ Rectangle r = new Rectangle(left, top, right-left, bot +tom-top); Bitmap cropped = canvas.Clone(r, canvas.PixelFormat); canvas.Dispose(); return cropped; }

In reply to Re: Cropping dependant on image contents by davidrw
in thread Cropping dependant on image contents by matthewsnape

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.