469,354 Members | 2,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

Auto-crop in PIL?

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.

--
Kevin Smith
Ke*********@sas.com
Jul 18 '05 #1
5 9183
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)
Jul 18 '05 #2
Tyler Eaves wrote:
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)


This only works if you actually know that the object you want to crop in
the middle is rectangular. If it's amorphous, you'll have to scan in
stripes.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ After a thousand years or so you go native.
\__/ Camden Benares
Jul 18 '05 #3
On Thu, 30 Oct 2003 12:54:37 -0800, Erik Max Francis wrote:
Tyler Eaves wrote:
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)


This only works if you actually know that the object you want to crop in
the middle is rectangular. If it's amorphous, you'll have to scan in
stripes..


I dunno, to me a crop is by definition rectangular, and that's the way
it's worked in every image editor I've ever used.

Jul 18 '05 #4
Tyler Eaves wrote:
I dunno, to me a crop is by definition rectangular, and that's the way
it's worked in every image editor I've ever used.


I think I misunderstood your intention, you had the right algorithm. I
thought you were suggesting that, for example, to find the left bounds
of the cropping rectangle you pick an arbitrary y coordinate and then
start increment x from 0 until you hit on a non-white pixel. That
wouldn't have worked, since you have to check all y coordinates in
strips in case the shape of the figure you wish to crop is irregular.

But on rereading your response, that probably wasn't what you were
suggesting, so never mind.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ We are victims of our circumstance.
\__/ Sade Adu
Jul 18 '05 #5
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.


import Image, ImageChops

##
# Crop borders off an image.
#
# @param im Source image.
# @param bgcolor Background color, using either a color tuple or
# a color name (1.1.4 only).
# @return An image without borders, or None if there's no actual
# content in the image.

def autocrop(im, bgcolor):
if im.mode != "RGB":
im = im.convert("RGB")
bg = Image.new("RGB", im.size, bgcolor)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
return None # no contents

</F>


Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Manlio Perillo | last post: by
5 posts views Thread by Robert Downes | last post: by
20 posts views Thread by Vijay Kumar R. Zanvar | last post: by
5 posts views Thread by maya | last post: by
22 posts views Thread by nospam_news | last post: by
2 posts views Thread by Piotr K | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.