Auto-crop in PIL?

K

Kevin Smith

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.
 
T

Tyler Eaves

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)
 
E

Erik Max Francis

Tyler said:
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.
 
T

Tyler Eaves

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.
 
E

Erik Max Francis

Tyler said:
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.
 
F

Fredrik Lundh

Kevin said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top