PIL (python imaging library) or Mathematics help both appreciated

Jorgen Grahn grahn+nntp at snipabacken.se
Mon Nov 24 16:09:53 CET 2008

On Mon, 24 Nov 2008 00:52:02 -0800 (PST), amine <surfeurX 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)
> 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
> to be more concrete:
> here is a bit of code of what i've made so far
> after the layer of transparency of the new image is done, the
> algorithm works generally fine except there are some small but
> noticeable quality issues.  i am just asking myself if there is maybe
> not a better approach either in terms of algorithms or even
> mathematics or maybe refine the algorithm that i've create.  anything
> would help.
> i know the function will not be 100% precise but I just hope the image
> can be presentable and that the image is homogenous.

How about calling on the Gimp? I haven't done it, but I seem to recall
it has a Python interface.  Hopefully that means you can use its
algorithms from a standalone Python program, with no GUI.

I guess in the Gimp you'd use a fuzzy "select a continuous region
around this pixel with approximately this color" algorithm. Then you'd
use another one which replaces the "whiteness" with transparency.


  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!

More information about the Python-list mailing list