PIL (python imaging library) or Mathematics help both appreciated

Arnaud Delobelle arnodel at googlemail.com
Mon Nov 24 04:57:04 EST 2008


Well not much maths in my answers but...

On 24 Nov, 08:52, amine <surfe... at gmail.com> wrote:
> well, here is the background.  I have images of objects (cars,
> clothes, ...) with a white background in most of the cases
>
> I have to build a function with PIL that takes away the background.
> it seems simple, just look for the "white" and make it transparent but
> the problem is in reality much more complex:
> 1) the image could contain some white inside the object (e.g. shoes
> with some white in between straps)

A simple solution would be to start with a transparent pixel in the
top left corner say, then scan the image from left to right (line by
line): if the current pixel is white (or pale enough) and has a
transparent pixel above it or to its left, then make it transparent.

I remember when I was a kid playing graphical adventure games on my
C64, you could actually see this happening as the picture was being
built on the screen (as a floppy could only contain around 160k, you
couldn't store bitmaps unless you had very few pictures).

> 2) there are often pixels that are part of the background but have a
> colour different from white which leaves a few points throughout the
> image

What you're doing is fine I think: calculate the distance to white.

HTH

--
Arnaud



More information about the Python-list mailing list