By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Auto-crop in PIL?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.