[Image-SIG] Using PIL to automatically trim borders from an image

Matt Howell matt at matthowell.com
Fri Jul 18 05:17:22 CEST 2008


Is there a straightforward way to use PIL to trim off the borders of an
image?

I'm thinking of something equivalent to the trim() function in ImageMagick.
It looks at the 4 corner pixels, figures out what color the border is, and
then trims out rows and columns from the image's edges that match the border
color.  So, as a use case, you could upload an image with some arbitrary
amount of whitespace around the main content of the image, and trim out all
that extra space.

I don't see any equivalent built-in functionality in the PIL documentation
-- crop() seems to come closest but it lacks the extra logic needed.  I'm
not opposed to writing the logic myself, but I'd like to avoid reinventing
the wheel, if I can. :)

Thanks for any ideas you can provide.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20080717/b634da55/attachment.htm>


More information about the Image-SIG mailing list