Auto-crop in PIL?

Tyler Eaves tyler at tylereaves.com
Thu Oct 30 12:49:05 EST 2003


On Thu, 30 Oct 2003 14:42:36 +0000, Kevin Smith wrote:

> Is there a way to "auto-crop" an image in PIL?  I have some PNG images 
> with white backgrounds that I want cropped to show only the non-white 
> portion in the center, but I don't see how I can do this.

Disclaimer: I've never used PIL, so I'm not familiar with it's interface.

Here's a basic algo:

>From each of the four directions, scan towards the center until you
encouter a non-white pixel. Then use those 4 intercepts as your corners.

That is, the image is in the rectangle (left non-white, top non-white),
(right non-white, top non-white), (right non-white, bottom non-white),
(left non-white, bottom non-white)




More information about the Python-list mailing list